简体   繁体   English

在Web API中设置“内容处置” HTTP标头

[英]Setting a “Content-Disposition” HTTP Header in Web API

I am trying to create integration test for my upload picture action. 我正在尝试为上传图片操作创建集成测试。 Raw request created from browser is like below; 从浏览器创建的原始请求如下所示;

POST /api/UpdateImage HTTP/1.1
Host: upload.qwe.com
Authorization: bearer KuThe6Wx/CW1TO/HVS+u3Tov3MRh8qTMDrSvQ09nMnP4OgYp
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=Boundary-D60385FA-C164-45B0-A81E-0F6488F8E1E1
Content-Length: 375488
Accept-Language: en-us
Accept: */*
Connection: keep-alive
User-Agent: Chrome
Pragma: no-cache
Cache-Control: no-cache

--Boundary-D60385FA-C164-45B0-A81E-0F6488F8E1E1
Content-Disposition: form-data; name="fileName"

image.jpg
--Boundary-D60385FA-C164-45B0-A81E-0F6488F8E1E1
Content-Disposition: form-data; name="fileUpload"; filename="image.jpg"
Content-Type: image/jpeg

And my code for integration test; 还有我的集成测试代码;

MultipartContent multipartContent = new MultipartContent();
 multipartContent.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=Boundary-D60385FA-C164-45B0-A81E-0F6488F8E1E1");
ContentDispositionHeaderValue contentDispositionHeaderValue = new ContentDispositionHeaderValue("form-data")
            {
                Name = "fileName"
            };
            multipartContent.Headers.ContentDisposition = contentDispositionHeaderValue;
// StreamContent
        FileStream fileStream = File.Open(@"./Assets/" + fileName, FileMode.Open);
        StreamContent stream = new StreamContent(fileStream);
        multipartContent.Add(stream);
httpRequestMessage.Content = multipartContent;
        return httpRequestMessage;

But I cannot set second part of the data which has Content-Disposition: form-data; name="fileUpload"; filename="image.jpg" 但是我不能设置具有Content-Disposition: form-data; name="fileUpload"; filename="image.jpg"的数据的第二部分Content-Disposition: form-data; name="fileUpload"; filename="image.jpg" Content-Disposition: form-data; name="fileUpload"; filename="image.jpg"

How can I achieve this? 我该如何实现?

Problem Summary: 问题摘要:

在此处输入图片说明

modify the sub HttpContent 's Headers, not in the MultipartFormDataContent 修改子HttpContent的标头,而不是在MultipartFormDataContent

        var main = new MultipartFormDataContent(Guid.NewGuid().ToString());
        HttpContent content = new StringContent("image.jpg");
        content.Headers.Clear();
        content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = "fileName" };
        main.Add(content);

        content = new StreamContent(new MemoryStream(new byte[] { 1, 2, 3 }));//your file stream, or other base64 string
        content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg");
        content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = "fileUpload", FileName="image.jpg" };
        main.Add(content);

        req.Content = main;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Web API 2中Content-Disposition始终为null - Content-Disposition is always null in web api 2 Content-Disposition 标头中的 Unicode - Unicode in Content-Disposition header HTTP标头(c#)中内容处置的替代方法 - Alternative to Content-Disposition in HTTP Header (c#) 从跨域 http.get 请求的 ASP.NET Web API 2 响应中获取 Angular 中的特定响应标头(例如,Content-Disposition) - Get a specific response header (e.g., Content-Disposition) in Angular from an ASP.NET Web API 2 response for a cross-origin http.get request 从 ASP.NET CORE Web Api 获取 React 中的特定响应标头(例如,Content-Disposition) - Get a specific response header (e.g., Content-Disposition) in React from an ASP.NET CORE Web Api Content-Disposition标头中的查询字符串 - Query string in Content-Disposition header .NET-WebAPI中未显示内容处置标头 - .NET - Content-Disposition Header Not Showing in WebAPI 内容处置 - 在邮件 header 中发现无效字符:'[' - Content-Disposition - an invalid character was found in the mail header: '[' 无法在 C# 中使用 httpclient 获取 header“内容处置” - Unable to get header "Content-Disposition" with httpclient in C# 在上传到 S3 的对象上设置`Content-Disposition`? - Setting `Content-Disposition` on Object uploaded to S3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM