Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

upload file using httpwebrequest c#

public static string UploadFilesToRemoteUrl(string url, string[] files, NameValueCollection formFields = null)
{
    string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");

    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
    request.ContentType = "multipart/form-data; boundary=" +
                            boundary;
    request.Method = "POST";
    request.KeepAlive = true;

    Stream memStream = new System.IO.MemoryStream();

    var boundarybytes = System.Text.Encoding.ASCII.GetBytes("
--" +
                                                            boundary + "
");
    var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("
--" +
                                                                boundary + "--");


    string formdataTemplate = "
--" + boundary +
                                "
Content-Disposition: form-data; name="{0}";

{1}";

    if (formFields != null)
    {
        foreach (string key in formFields.Keys)
        {
            string formitem = string.Format(formdataTemplate, key, formFields[key]);
            byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
            memStream.Write(formitembytes, 0, formitembytes.Length);
        }
    }

    string headerTemplate =
        "Content-Disposition: form-data; name="{0}"; filename="{1}"
" +
        "Content-Type: application/octet-stream

";

    for (int i = 0; i < files.Length; i++)
    {
        memStream.Write(boundarybytes, 0, boundarybytes.Length);
        var header = string.Format(headerTemplate, "uplTheFile", files[i]);
        var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

        memStream.Write(headerbytes, 0, headerbytes.Length);

        using (var fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
        {
            var buffer = new byte[1024];
            var bytesRead = 0;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                memStream.Write(buffer, 0, bytesRead);
            }
        }
    }

    memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
    request.ContentLength = memStream.Length;

    using (Stream requestStream = request.GetRequestStream())
    {
        memStream.Position = 0;
        byte[] tempBuffer = new byte[memStream.Length];
        memStream.Read(tempBuffer, 0, tempBuffer.Length);
        memStream.Close();
        requestStream.Write(tempBuffer, 0, tempBuffer.Length);
    }

    using (var response = request.GetResponse())
    {
        Stream stream2 = response.GetResponseStream();
        StreamReader reader2 = new StreamReader(stream2);
        return reader2.ReadToEnd();
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to get current dir in c# 
Csharp :: how to add rigidbody in unity 
Csharp :: rotate gameobject unity 
Csharp :: Show private fields in Unity Inspector 
Csharp :: C# new form 
Csharp :: unity gui style color button 
Csharp :: if set active == false 
Csharp :: c# html to pdf 
Csharp :: how to minimum text length in textbox in c# 
Csharp :: random mac address c# 
Csharp :: get all classes that extend a class c# 
Csharp :: string to char array c# 
Csharp :: select random from enum c# 
Csharp :: wasd code for unity 
Csharp :: interpolate rotation unity3d 
Csharp :: Plugging a Third-Party IoC Container (e.g. AutoFac) into .NET Core 6 
Csharp :: should i learn c # 
Csharp :: how set cascade on delete and update in same time in laravel 
Csharp :: #ifdef in c 
Csharp :: LINQ: 2 join with group by 
Csharp :: exception handling in c# web api 
Csharp :: window height in C# forms 
Csharp :: expansion tile 
Csharp :: c# method returns multiple values 
Csharp :: searching for keys in the registry 
Csharp :: How can I display image from database in asp.net mvc. I created image table and image path as varchar 
Csharp :: label wpf 
Csharp :: c# create class from parent class 
Csharp :: push c# array 
Csharp :: get after point in c# 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =