Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# http post request with file

string fileLocation = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + Path.DirectorySeparatorChar + "somefile.jpg";
NameValueCollection values = new NameValueCollection();
NameValueCollection files = new NameValueCollection();
values.Add("firstName", "Alan");
files.Add("profilePicture", fileLocation);
sendHttpRequest("http://example.com/handler.php", values, files);
Comment

C# http post request with file

// Example to how you could handle it in PHP
echo $_POST['firstName'];
$name = $_POST['firstName'];
$image = $_FILES['profilePicture'];
$ds = DIRECTORY_SEPARATOR;
move_uploaded_file($image['tmp_name'], realpath(dirname(__FILE__)) . $ds . "uploads" . $ds . $image['name']);
Comment

C# http post request with file

private static string sendHttpRequest(string url, NameValueCollection values, NameValueCollection files = null)
{
    string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
    // The first boundary
    byte[] boundaryBytes = System.Text.Encoding.UTF8.GetBytes("
--" + boundary + "
");
    // The last boundary
    byte[] trailer = System.Text.Encoding.UTF8.GetBytes("
--" + boundary + "--
");
    // The first time it itereates, we need to make sure it doesn't put too many new paragraphs down or it completely messes up poor webbrick
    byte[] boundaryBytesF = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "
");

    // Create the request and set parameters
    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
    request.ContentType = "multipart/form-data; boundary=" + boundary;
    request.Method = "POST";
    request.KeepAlive = true;
    request.Credentials = System.Net.CredentialCache.DefaultCredentials;

    // Get request stream
    Stream requestStream = request.GetRequestStream();

    foreach (string key in values.Keys)
    {
        // Write item to stream
        byte[] formItemBytes = System.Text.Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name="{0}";

{1}", key, values[key]));
        requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
        requestStream.Write(formItemBytes, 0, formItemBytes.Length);
    }

    if (files != null)
    { 
        foreach(string key in files.Keys)
        {
            if(File.Exists(files[key]))
            {
                int bytesRead = 0;
                byte[] buffer = new byte[2048];
                byte[] formItemBytes = System.Text.Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name="{0}"; filename="{1}"
Content-Type: application/octet-stream

", key, files[key]));
                requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                requestStream.Write(formItemBytes, 0, formItemBytes.Length);

                using (FileStream fileStream = new FileStream(files[key], FileMode.Open, FileAccess.Read))
                {
                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        // Write file content to stream, byte by byte
                        requestStream.Write(buffer, 0, bytesRead);
                    }

                    fileStream.Close();
                }
            }
        }
    }

    // Write trailer and close stream
    requestStream.Write(trailer, 0, trailer.Length);
    requestStream.Close();

    using (StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream()))
    {
        return reader.ReadToEnd();
    };
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: convert list to datatable c# 
Csharp :: c# short to int 
Csharp :: unity stop velocity movement 
Csharp :: Unlit shader get the direction of camera UNity 
Csharp :: wpf keydown detect if control key is down 
Csharp :: #grid 
Csharp :: c# convert excel column index to letter 
Csharp :: trhow exception if is null c# 
Csharp :: c# recorrer una lista 
Csharp :: c# unit test exception using try catch 
Csharp :: c# decimal 4 casas decimais 
Csharp :: Non-Serialized Fields in unity inspector 
Csharp :: asp.net call controller from another controller 
Csharp :: c# in equivalent 
Csharp :: dctionary literal c# 
Csharp :: subtract to time c# 
Csharp :: escape chars for regex c# 
Csharp :: if or statement c# 
Csharp :: unity screen size fix 
Csharp :: messagebox yes no c# 
Csharp :: generic interface c# 
Csharp :: excel rows count 
Csharp :: .net using appsettings variables 
Csharp :: assert.equal 
Csharp :: error cs1585 unity 
Csharp :: Moq Unittest with ILogger 
Csharp :: Count Rows of table using Linq 
Csharp :: SceneManagment by BuildIndex 
Csharp :: clear highlight winforms treeview 
Csharp :: c# multipthreading 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =