0

I'm trying to upload some files to a server.

My code looks like:

public void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc, string cookie)
{
try
{
    string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
    byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

    SetStatus("Making request");
    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
    wr.ContentType = "multipart/form-data; boundary=" + boundary;
    wr.Method = "POST";
    wr.Headers["Cookie"] = cookie;
    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}";
    SetStatus("Parsing values");
    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);

    SetStatus("Reading file");
    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();

    SetStatus("Sending request");
    byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
    rs.Write(trailer, 0, trailer.Length);
    rs.Close();

    HttpWebResponse wresp = null;
    wresp = (HttpWebResponse)wr.GetResponse();
    if (wresp.StatusCode != HttpStatusCode.OK)
    {
        throw new Exception("File was not uploaded successfully.");
    }
    SetStatus("Success");
}
catch
{
    statusLabel.ForeColor = Color.Red;
    SetStatus("Failure");
}

}

An call it in this way:

void uploader_DoWork2(object sender, DoWorkEventArgs e)
{
    NameValueCollection c = new NameValueCollection();
    c.Add("chunk", "0");
    c.Add("name", Path.GetFileName(filenameBox.Text));
    c.Add("chunks", "1");
    HttpUploadFile("https://site.com/upload.php", filenameBox.Text, "file", GetContentType(filenameBox.Text), c, loginCookies);
}

The script works fine and files are uploaded if these are < 2MB. If the file is larger than 2MB it return success even if the file was not successfully uploaded. I think the files larger than 2MB should be splited in chunks and then send to server. But I have no idea how to split file then send to server in chunks...

caffeine
  • 435
  • 7
  • 26
  • I saw you used HTTP upload. So ould you talk to your system administrator to uplift the upload file size a little bit? 2MB is quite a small size. – Jian Huang Mar 19 '14 at 20:52
  • I'm the administrator of the site. How to do that? I will edit and post code above... Script is not made by me, this is why I ask,,, – caffeine Mar 19 '14 at 20:54
  • You can change the setting in web.config. Here is a link in SO. http://stackoverflow.com/questions/288612/how-to-increase-the-max-upload-file-size-in-asp-net – Jian Huang Mar 19 '14 at 21:03
  • I think is not that. From WHM was already set 50MB cPanel PHP max upload size. I think file must be splited and send chunk by chunk to server... – caffeine Mar 19 '14 at 21:13

0 Answers0