I have the method HttpUploadFile
to upload an image with some string parameters:
public static void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
{
Console.Write(string.Format("Uploading {0} to {1}", file, url));
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
byte[] omg = boundarybytes;
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream rs = wr.GetRequestStream();
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
}
rs.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate, paramName, file, contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
//parsing response
WebResponse wresp = null;
try
{
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
String output = reader2.ReadToEnd();
}
catch (Exception ex){
Console.Write("Error uploading file" + ex.ToString());
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}
}
And now I want to track the progress of uploading. I've found C# HttpWebRequest Form Post with Progress Tracking (for uploading potentially large files) but that solution doesn't work for me.
You are reading your image out of a file and uploading it 4096 bytes at a time. After each 4096 bytes you could call some other function that updates a progress bar to the new value.
First you will need to know how large the file is, in total. Whenever a package has been uploaded you can calculate the percentage, by summing up all sent packages and comparing them to the total amount of bytes to be sent.
As I just see from the thread you linked, there could be a problem with HttpWebRequest not actually sending the data, before you call GetResponse.
To prevent that you can try to make it send the data right away, by using some workaround like setting the Content-Length (wich means you need to know how much data you are sending beforehand), or possibly by setting the transfer-encoding to chunked (dont know if that works, though). You could also just use a raw socket, so you have total control. Its a little more work to send the request like that, but not very complicated.
If I have some time later on and you decide to go with a raw socket, I could put a small example together for you.