带有多个文件的POST StreamContent [英] POST StreamContent with Multiple Files

查看:150
本文介绍了带有多个文件的POST StreamContent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码段已成功HttpPost将单个文件发布到WebAPI.我想对其进行扩展以构建包含多个文件的StreamContent(类似于Fiddler多文件发布).

The code snippet below successfully HttpPosts a single file to WebAPI. I'd like expand it to build StreamContent containing multiple files (similar to Fiddler multi-file posts).

我知道我应该在StreamContent上添加一个边界",但是我不确定确切的位置.我想最终将FileStream/Stream参数转换为List,以便我可以遍历集合并将StreamContent构建为POST.

I know I should be adding a "boundary" to the StreamContent, but I'm not sure exactly where. I'd like to eventually convert the FileStream/Stream parameters to be a List so I can iterate through the collection and build StreamContent to POST.

请告诉我该帖子是否有意义.我将不胜感激任何建议.

Let me know if this post makes any sense. I'd appreciate any suggestions.

提前谢谢!

public async Task<HttpStatusCode> UploadOrderFile(FileStream imageFileStream, string filename, string contentType = "image/png")
    {
        JsonApiClient._client.DefaultRequestHeaders.Clear();
        var content = new MultipartFormDataContent
        {
            JsonApiClient.CreateFileContent(imageFileStream, filename, contentType)
        };
        JsonApiClient._client.DefaultRequestHeaders.Add("Authorization",
            " Bearer " + JsonApiClient.Token.AccessToken);
        var response = await JsonApiClient._client.PostAsync("api/UploadFile", content);

        response.EnsureSuccessStatusCode();
        return response.StatusCode;
    }

internal static StreamContent CreateFileContent(Stream stream, string fileName, string contentType)
    {
        var fileContent = new StreamContent(stream);
        fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
        {
            Name = "\"files\"",
            FileName = "\"" + fileName + "\""
        }; 
        fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
        return fileContent;
    }

接收和保存发布的文件没有任何问题.问题在于创建发布多个文件所必需的StreamContent.

推荐答案

这是我尝试过的适用于我的解决方案. CreateFileContent中没有任何更改.我只是简单地将参数转换为集合,遍历每个集合,并从多个StreamContent中添加了新的MultiPartFormDataContent.边界也已添加到MultipartFormDataContent对象.如果您发现任何效率低下或完全错误的内容,请告诉我.谢谢!

This is a solution that I tried that works for me. Nothing was changed in CreateFileContent. I just simply converted parameters into collections, iterated through each collection, and added new MultiPartFormDataContent out of multiple StreamContent. The boundary was also added to the MultipartFormDataContent object. If you see anything that is inefficient or plain wrong, let me know. Thanks!

public async Task<HttpStatusCode> UploadOrderFile(List<FileStream> imageFileStream, List<string> filename, string salesOrderNo, List<string> contentType)
    {
        JsonApiClient._client.DefaultRequestHeaders.Clear();
        var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
        var content = new MultipartFormDataContent(boundary); 
        for (var i = 0; i < imageFileStream.Count; i++)
        {
            content.Add(JsonApiClient.CreateFileContent(imageFileStream[i], filename[i], contentType[i]));
        }

        JsonApiClient._client.DefaultRequestHeaders.Add("Authorization",
            " Bearer " + JsonApiClient.Token.AccessToken);
        var response = await JsonApiClient._client.PostAsync("api/UploadFile", content);

        response.EnsureSuccessStatusCode();
        return response.StatusCode;
    }

internal static StreamContent CreateFileContent(Stream stream, string fileName, string contentType)
{
    var fileContent = new StreamContent(stream);
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
    {
        Name = "\"files\"",
        FileName = "\"" + fileName + "\""
    }; 
    fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
    return fileContent;
}

这篇关于带有多个文件的POST StreamContent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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