Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Upload files with HTTPWebrequest (multipart/form-data)

    • 0
    • 2
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 5.33k
    Comment on it

    Hi, Some times we need to upload files on a web server using HttpWebRequest.

    For that first we need to define a model (class) that represents the uploaded file property.

    1. public class UploadFile
    2. {
    3. public string Name { get; set; }
    4. public string Filename { get; set; }
    5. public string ContentType { get; set; }
    6. public Stream Stream { get; set; }
    7. }

    Use below sample code for uploading files using httpWebRequest.

    1. public byte[] UploadFilesToServer(string address, IEnumerable files, NameValueCollection values)
    2. {
    3. var request = WebRequest.Create(address);
    4. request.Method = "POST";
    5. var boundary = "---------------------------" + DateTime.Now.ToString("a", NumberFormatInfo.InvariantInfo);
    6. request.ContentType = "multipart/form-data; boundary=" + boundary;
    7. boundary = "--" + boundary;
    8.  
    9. using (var requestStream = request.GetRequestStream())
    10. {
    11. // Write the values
    12. foreach (string name in values.Keys)
    13. {
    14. var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
    15. requestStream.Write(buffer, 0, buffer.Length);
    16. buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
    17. requestStream.Write(buffer, 0, buffer.Length);
    18. buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
    19. requestStream.Write(buffer, 0, buffer.Length);
    20. }
    21.  
    22. // Write the files
    23. foreach (var file in files)
    24. {
    25. var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
    26. requestStream.Write(buffer, 0, buffer.Length);
    27. buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
    28. requestStream.Write(buffer, 0, buffer.Length);
    29. buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
    30. requestStream.Write(buffer, 0, buffer.Length);
    31. file.Stream.CopyTo(requestStream);
    32. buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
    33. requestStream.Write(buffer, 0, buffer.Length);
    34. }
    35.  
    36. var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
    37. requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
    38. }
    39.  
    40. using (var response = request.GetResponse())
    41. using (var responseStream = response.GetResponseStream())
    42. using (var stream = new MemoryStream())
    43. {
    44. responseStream.CopyTo(stream);
    45. return stream.ToArray();
    46. }
    47. }
    48.  

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: