Submitting File and Json data to webapi from HttpC

2020-02-06 17:00发布

问题:

I want to send file and json data from HttpClient to web api server.
I cant seem to access the json in the server via the payload, only as a json var.

 public class RegulationFilesController : BaseApiController
    {
        public void PostFile(RegulationFileDto dto)
        {
            //the dto is null here
        }
    }

here is the client:

   using (var client = new HttpClient())
                {
                    using (var content = new MultipartFormDataContent())
                    {
                        client.BaseAddress = new Uri(ConfigurationManager.AppSettings["ApiHost"]);
                        content.Add(new StreamContent(File.OpenRead(@"C:\\Chair.png")), "Chair", "Chair.png");
                        var parameters = new RegulationFileDto
                        {
                            ExternalAccountId = "1234",
                        };
                        JavaScriptSerializer serializer = new JavaScriptSerializer();
                        content.Add(new StringContent(serializer.Serialize(parameters), Encoding.UTF8, "application/json"));

                            var resTask = client.PostAsync("api/RegulationFiles", content); //?ApiKey=24Option_key
                            resTask.Wait();
                            resTask.ContinueWith(async responseTask =>
                            {
                                var res = await responseTask.Result.Content.ReadAsStringAsync();


                            }
                                );

                    }
                }

this example will work:HttpClient Multipart Form Post in C# but only via the form-data and not payload.

Can you please suggest how to access the file and the submitted json And the file at the same request?

Thanks

回答1:

I have tried many different ways to submit both file data and metadata and this is the best approach I have found:

Don't use MultipartFormDataContent, use only StreamContent for the file data. This way you can stream the file upload so you don't take up too much RAM on the server. MultipartFormDataContent requires you to load the entire request into memory and then save the files to a local storage somewhere. By streaming, you also have the benefit of copying the stream into other locations such as an Azure storage container.

This solves the issue of the binary data, and now for the metadata. For this, use a custom header and serialize your JSON into that. Your controller can read the custom header and deserialize it as your metadata dto. There is a size limit to headers, see here (8-16KB), which is a large amount of data. If you need more space, you could do two separate requests, one to POST the minimum need, and then a PATCH to update any properties that needed more than a header could fit.

Sample code:

public class RegulationFilesController : BaseApiController
{
    public async Task<IHttpActionResult> Post()
    {
        var isMultipart = this.Request.Content.IsMimeMultipartContent();

        if (isMultipart)
        {
            return this.BadRequest("Only binary uploads are accepted.");
        }

        var headerDto = this.GetJsonDataHeader<RegulationFileDto>();
        if(headerDto == null)
        {
            return this.BadRequest("Missing X-JsonData header.");
        }

        using (var stream = await this.Request.Content.ReadAsStreamAsync())
        {
            if (stream == null || stream.Length == 0)
            {
                return this.BadRequest("Invalid binary data.");
            }

            //save stream to disk or copy to another stream

            var model = new RegulationFile(headerDto);

            //save your model to the database

            var dto = new RegulationFileDto(model);

            var uri = new Uri("NEW URI HERE");

            return this.Created(uri, dto);
        }
    }

    private T GetJsonDataHeader<T>()
    {
        IEnumerable<string> headerCollection;

        if (!this.Request.Headers.TryGetValues("X-JsonData", out headerCollection))
        {
            return default(T);
        }

        var headerItems = headerCollection.ToList();

        if (headerItems.Count() != 1)
        {
            return default(T);
        }

        var meta = headerItems.FirstOrDefault();

        return !string.IsNullOrWhiteSpace(meta) ? JsonConvert.DeserializeObject<T>(meta) : default(T);
    }
}