我正在尝试将一个大文件(大约 40 MB 到 50MB)发布到 mvc rest api。我可以使用 RestSharp restclient 来实现这一点,但是在使用 AddFile 时,它总是添加不需要的开始和尾随文本。
我后来尝试了HttpWebRequest和HttpClient,它们都无法写入完整的文件流。
我已经尝试了该站点上的所有示例,但没有任何效果。
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(requestUrl);
wr.ContentType = "application/octet-stream";
wr.Method = "POST";
wr.KeepAlive = true;
Stream rs = wr.GetRequestStream();
FileStream fileStream = new FileStream(xmlConfigurationFilePath, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
rs.Flush();
WebResponse wresp = null;
try
{
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
}
catch (Exception ex)
{
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}