【发布时间】:2013-04-14 03:48:55
【问题描述】:
我正在尝试创建一个对我的服务器的 api 调用,以允许我上传一个 .csv 文件。
客户端代码
string url = "http://testserver/testapi";
string filePath = @"C:\test.csv";
using (WebClient wc = new WebClient())
{
wc.Headers.Add("Content-Type", "text/plain");
byte[] responseArray = wc.UploadFile(url, filePath);
}
服务器端代码
string savePath = testSavePath;
foreach (string f in Request.Files.AllKeys)
{
HttpPostedFileBase file = Request.Files[f];
file.SaveAs(savePath);
}
我在这条线上遇到了异常byte[] responseArray = wc.UploadFile(url, filePath);
奇怪的是,当我看Request时,我看到了
ContentType="multipart/form-data; boundary=---------------------8d006b7c2a5452c".
查看 UploadFile 的文档,我发现The Content-type header begins with multipart. 时会引发 WebException
我的问题是为什么 contentType 被设置为 multipart 以及如何防止它这样做?
【问题讨论】:
-
你遇到了什么异常?
-
去掉
wc.Headers.Add("Content-Type", "text/plain");还能用吗?
标签: c# post file-upload