Arun Kumar Arun Kumar - 8 months ago 24
ASP.NET (C#) Question

Upload files using ASP.NET

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" />
<button type="submit">submit</button>

The above code uploads the files to file sonic server, but I want to do this using programmatically using C#, basically my requirement is that the program creates the form and file control and sends the file to the Filesonic server URL mentioned in action attribute..

I have gone through many links but with no success, I have gone through the following links with no success.

Upload files with HTTPWebrequest (multipart/form-data)


The following code will upload the file to the server as long as the server can accept it outside of files[] array.

WebRequest webRequest = WebRequest.Create("");
FileStream reader = new FileStream("file_to_upload", FileMode.Open);

byte[] data = new byte[reader.Length];
webRequest.Method = "POST";
webRequest.ContentType = "multipart/form-data";
webRequest.ContentLength = reader.Length;
webRequest.AllowWriteStreamBuffering = "true";

reader.Read(data, 0, reader.Length);

using (var request = webRequest.GetRequestStream())
    request.Write(data, 0, data.Length);
    using (var response = webRequest.GetResponse())
        //Do something with response if needed