如何阅读.Net 4.0中的MultipartContent()

时间:2018-06-11 08:13:58

标签: asp.net-web-api multipart

我在.Net 4.0中创建了需要阅读MultipartContent()

的ASP.NET方法

Web API,.Net 4.5

public HttpResponseMessage Get(int id) { var content = new MultipartContent(); var ids = new List<int>() { 1, 2 };

        var objectContent = new ObjectContent<List<int>>(ids, new System.Net.Http.Formatting.JsonMediaTypeFormatter());
        content.Add(objectContent);

        var file1Content = new StreamContent(new FileStream(@"c:\API\desert.jpg", FileMode.Open));
        file1Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("image/jpeg");
        file1Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        file1Content.Headers.ContentDisposition.FileName = "desert.jpg";
        content.Add(file1Content);

        var file2Content = new StreamContent(new FileStream(@"c:\API\test.txt", FileMode.Open));
        file2Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/plain");
        file2Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        file2Content.Headers.ContentDisposition.FileName = "test.txt";
        content.Add(file2Content);

        var response = new HttpResponseMessage();
        response.Content = content;

        return response;
    }`

是否有更简单的方法来读取.Net 4.0中的所有返回文件

0 个答案:

没有答案