Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Request.Content.IsMimeMultipartContent()失败_C#_Asp.net Web Api_Http Post - Fatal编程技术网

C# Request.Content.IsMimeMultipartContent()失败

C# Request.Content.IsMimeMultipartContent()失败,c#,asp.net-web-api,http-post,C#,Asp.net Web Api,Http Post,我正在尝试使用Html2 input type=“file”和angular2 http.post请求上载一个文件。当请求到达web api时,它在 Request.Content.IsMimeMultipartContent() 当使用Postman提交请求时,不会失败(因为Postman会处理请求,所以我不在标题中包含内容类型) 请参阅我的代码: Html: 和WebApi post请求(在 如果(!Request.Content.IsMimeMultipartContent())): p

我正在尝试使用Html2 input type=“file”和angular2 http.post请求上载一个文件。当请求到达web api时,它在

Request.Content.IsMimeMultipartContent()
当使用Postman提交请求时,不会失败(因为Postman会处理请求,所以我不在标题中包含内容类型)

请参阅我的代码: Html:

和WebApi post请求(在 如果(!Request.Content.IsMimeMultipartContent())):

public异步任务PostFormData()
{
//检查请求是否包含多部分/表单数据。
如果(!Request.Content.IsMimeMultipartContent())//在此处失败
{
抛出新的HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root=HttpContext.Current.Server.MapPath(“~/App_Data”);
var provider=新的MultipartFormDataStreamProvider(根);
尝试
{
//读取表单数据。
wait Request.Content.ReadAsMultipartAsync(提供程序);
//这说明了如何获取文件名。
foreach(provider.FileData中的MultipartFileData文件)
{
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine(“服务器文件路径:“+file.LocalFileName”);
}
返回Request.CreateResponse(HttpStatusCode.OK);
}
捕获(System.e例外)
{
返回请求.CreateErrorResponse(HttpStatusCode.InternalServerError,e);
}
}

经过彻底的研究-我成功了: 发布时无需设置内容类型标题属性。
我已经从angular2 http.post请求和
传递的web api post方法中的Request.Content.IsMimeMultipartContent()(与postman中的方法相同)

如果其他任何人遇到此“此资源不支持请求实体的媒体类型“multipart/form data”

您可能需要将其添加到webapiconfig中

 config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("multipart/form-data"));

这让我有点困惑,很感激,有什么例外?MS文档没有列出例外情况。
uploadFile(event) {
    let fileUploadUrl = this.webApiFileUploadURL;
    let fileList: FileList = event.target.files;
    if(fileList.length > 0) {
        let file: File = fileList[0];
        let formData:FormData = new FormData();
        formData.append('uploadFile', file, file.name);
        let headers = new Headers();
        headers.append('Content-Type', 'multipart/form-data');
        headers.append('Accept', 'application/json');
        let options = new RequestOptions({ headers: headers });
        this._http.post(`${this.webApiFileUploadURL}`, formData, options)
            .map(res => res.json())
            .catch(error => Observable.throw(error))
            .subscribe(
                data => console.log('success'),
                error => console.log(error)
            )
    }
public async Task<HttpResponseMessage> PostFormData()
    {

        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent()) // Fails here 
        {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        try
        {
            // Read the form data.
            await Request.Content.ReadAsMultipartAsync(provider);

            // This illustrates how to get the file names.
            foreach (MultipartFileData file in provider.FileData)
            {
                Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                Trace.WriteLine("Server file path: " + file.LocalFileName);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (System.Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }
    }
 config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("multipart/form-data"));