hi!
since WebClient.UploadFile() always uploads the file into the form named "file", i can't use it. so i made my own function:
you see the exception handler? thats where stream.Write() causes an exception "connection was aborted; connection was canceled".Code:static private string UploadFile(string url, string path) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); string boundary = DateTime.Now.Ticks.ToString("x"); byte[] boundary_ = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); string header = "Content-Disposition: form-data; name=\"archivo\"; filename=\"" + Path.GetFileName(path) + "\"\r\nContent-Type: application/octet-stream\r\n\r\n"; byte[] header_ = Encoding.ASCII.GetBytes(header); req.ContentType = "multipart/form-data; boundary=" + boundary; req.Method = "POST"; req.KeepAlive = true; req.AllowAutoRedirect = false; req.ContentLength = boundary_.Length * 2 + header_.Length + new FileInfo(path).Length; Stream stream = req.GetRequestStream(); stream.Write(boundary_, 0, boundary_.Length); stream.Write(header_, 0, header_.Length); FileStream file = File.OpenRead(path); byte[] buffer = new byte[4096]; int read = 0; int prg = 0; int count = 0; DateTime last = DateTime.Now; while ((read = file.Read(buffer, 0, buffer.Length)) > 0) { try { stream.Write(buffer, 0, read); } catch { SharedFunctions.Error("The server rejected the file at a status of " + SharedFunctions.SizeName(prg) + "/" + SharedFunctions.SizeName(Convert.ToInt32(file.Length)) + ".\nThis could be because the file exceeded the maximum upload file size of the server."); stream = null; break; } prg += read; count += read; int per = prg * 100 / Convert.ToInt32(file.Length); prgUpload.Value = per; lblUploadProgress.Text = per + " %"; if ((DateTime.Now - last).Seconds > 0) { last = DateTime.Now; lblUploadSpeed.Text = SharedFunctions.SizeName(count) + "/s"; lblUploadTimeleft.Text = Convert.ToString(TimeSpan.FromSeconds(Convert.ToDouble((file.Length - prg) / count))); count = 0; } Application.DoEvents(); } file.Close(); if (stream != null) { stream.Write(boundary_, 0, boundary_.Length); stream.Close(); WebResponse res = req.GetResponse(); string ret = res.Headers[HttpResponseHeader.Location]; res.Close(); return ret; } else { return ""; } }
this usually happens to files above ~3 MB.
does anyone know why this keeps happening?
thank you so much!



LinkBack URL
About LinkBacks



