c#设置uploadFile内容类型 [英] c# setting uploadFile content-Type

查看:234
本文介绍了c#设置uploadFile内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个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);



$ b $ p

$ b

服务器端代码 pre> string savePath = testSavePath;
foreach(在Request.Files.AllKeys中的字符串f)
{
HttpPostedFileBase file = Request.Files [f];
file.SaveAs(savePath);



$ b $ p
我在这一行收到一个异常 byte [ ] responseArray = wc.UploadFile(url,filePath);



奇怪的是当我看 Request 我看见

ContentType =multipart / form-data; boundary = --------- ------------ 8d006b7c2a5452c



查看UploadFile的文档,我发现WebException将会是抛出内容类型头以multipart开始。



我的问题是为什么contentType被设置为multipart我该如何防止这样做呢? 解决方案

您的客户端代码似乎没问题。异常可能来自服务器端。但是这很难判断,因为你没有分享这个例外。请问,你会得到更好的答案。



对于多部分内容类型: WebClient.UploadFile Content-Type 头文件的内容类型(默认为 application / octet-stream 如果没有设置)一个完整的HTTP请求。实际的HTTP请求总是(如你所见) multipart / form-data (根据上传文件的HTTP规范),你不能(也不应该)改变它。文档中的注释是指实际文件的内容类型。这意味着你不能要求 WebClient 上传自己的多部分的文件(这将被包装在另一个多部分HTTP信封中)。


I am trying to create an api call to my server that will allow me to upload a .csv file.

client side code

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);
}

server side code

string savePath = testSavePath;
foreach (string f in Request.Files.AllKeys)
{
    HttpPostedFileBase file = Request.Files[f];
    file.SaveAs(savePath);
}

I am getting an exception on this line byte[] responseArray = wc.UploadFile(url, filePath);

The strange thing is that when I look at Request I see

ContentType="multipart/form-data; boundary=---------------------8d006b7c2a5452c".

Looking at the docs for UploadFile I see that a WebException will be thrown when The Content-type header begins with multipart.

My question is why is contentType getting set to multipart and how do I prevent it from doing so?

解决方案

Your client code seems ok. The exception may be coming from the server-side. But that's hard to judge as you did not share the exception. Please do, you will get better answers.

For the multipart content type: The WebClient.UploadFile uses value of Content-Type header for content type of the file (defaulting to application/octet-stream if not set), not a whole HTTP request. The actual HTTP request is always (as you have found) multipart/form-data (as per HTTP specification for uploading files) and you cannot (and should not) change it. The note in documentation refers to content type of the actual file. It means that you cannot ask WebClient to upload file that is multipart on its own (that would be wrapped in another multipart HTTP envelope).

这篇关于c#设置uploadFile内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆