HttpWebRequest works. WebClient.UploadFile doesn't

后端 未结 1 1659
失恋的感觉
失恋的感觉 2021-01-16 05:46

I thought I figured out a way to simplify my code by using WebClient.UploadFile instead of HttpWebRequest, but I end up getting a file on the server end tha

相关标签:
1条回答
  • 2021-01-16 06:01

    WebClient.UploadFile sends the data in a multipart/form-data format. What you want to use to have the equivalent to the code using HttpWebRequest is the WebClient.UploadData method:

    var client = new WebClient();
    client.Encoding = Encoding.UTF8;
    client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
    byte[] fileContents = File.ReadAllBytes(filename);
    client.UploadData(new Uri("http://" + ConnectionManager.FileServerAddress + ":" +
           ConnectionManager.FileServerPort +
           "/binary/up/" + category + "/" +
           Path.GetFileName(filename) + "/" + safehash), fileContents);
    
    0 讨论(0)
提交回复
热议问题