0

I am making a C# console application to encrypt and upload files to Backblaze B2 backup. I am using HttpWebRequest to upload files using their API, however, I get an IOException "Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host." from stream.Write when uploading anything bigger than 4MB. I have to use a HTTP POST request as per the B2 API, so is there a way to get past .Net's 4MB limit?

My relevant code is

internal void UploadFile(FileInfo f, string sha1) {
    HttpWebRequest webRequest = WebRequest.CreateHttp(this.uploadUrl);
    webRequest.Method = "POST";
    webRequest.Headers.Add("Authorization", this.uploadAuthorizationToken);
    webRequest.Headers.Add("X-Bz-File-Name", f.Name);
    webRequest.Headers.Add("X-Bz-Content-Sha1", sha1);
    webRequest.ContentType = "application/octet-stream";
    webRequest.ContentLength = f.Length;
    byte[] bytes = File.ReadAllBytes(f.FullName);
    using (var stream = webRequest.GetRequestStream()) {
        stream.Write(bytes, 0, bytes.Length); //IOException occurs here
        stream.Close();
    }
    WebResponse response = (HttpWebResponse)webRequest.GetResponse();
    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
    response.Close();
    Console.WriteLine("b2_upload_file:\n" + responseString);

}

And the documentation for b2_upload_file https://www.backblaze.com/b2/docs/b2_upload_file.html

Daniel
  • 9,491
  • 12
  • 50
  • 66
vsong
  • 23
  • 1
  • 6
  • 1
    Could you please clarify why approaches you've found by searching https://www.bing.com/search?q=.net+upload+limit like http://stackoverflow.com/questions/288612/how-to-increase-the-max-upload-file-size-in-asp-net did not work? – Alexei Levenkov Dec 26 '15 at 07:02
  • 1
    Possible duplicate http://stackoverflow.com/questions/288612/how-to-increase-the-max-upload-file-size-in-asp-net – Daniel Dec 26 '15 at 07:09
  • @AlexeiLevenkov I have tried those solutions but they do not work. This is not a ASP.NET application nor do I control the host (Backblaze does, but they probably don't have a 4MB limit anyways since you can upload up to 5GB files according to them). – vsong Dec 26 '15 at 15:12
  • I thought so. You need to find sample that works and figure out what it uses to upload large files - likely upload is split into multiple requests... You can use Fiddler to look at requests. When you know what need to be done - clarify/ask new question how to do that in .NET (as sample you'll find likely be in JavaScript/curl/python) – Alexei Levenkov Dec 26 '15 at 20:58

0 Answers0