如何在ASP.NET C#上接收MultipartFormData [英] How to receive MultipartFormData on ASP.NET C#

查看:1125
本文介绍了如何在ASP.NET C#上接收MultipartFormData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和一个iOS家伙一起工作.他想通过WebAPI ASP.NET上传图像.我必须打个电话才能接收这些图像.
他说他正在使用AFNetworking通过AFMultipartFormData发送数据.我的问题是,我最后怎么能收到这个?我应该以JSON格式获取数据吗?或为此目的需要采取什么措施?我想知道整个过程,因为这是我第一次使用MultipartFormData.

UPDATE
基于我使用的答案:

I'm working with an iOS guy. He wants to upload images through WebAPI ASP.NET. I've to make a call that can receive those images.
He said he is using AFNetworking to send data through AFMultipartFormData. My question is that how can I receive this at my end? Should I take the data in JSON format? Or what measures needs to be done for this purpose? I want to know the whole process as this is my first time working with MultipartFormData.

UPDATE
Based on the answer I used this:

[HttpPut]
        public IHttpActionResult GetPatientFilesAction(int id, Model.Patients.PatientFiles patientFile)
        {
            Model.Patients.PatientFiles pFile=new Model.Patients.PatientFiles();
            try
            {
                HttpPostedFile xmlFile = HttpContext.Current.Request.Files[0];

                var fileForm = HttpContext.Current.Request.Form;
                var fileKey = HttpContext.Current.Request.Form.Keys[0];
                string[] jsonformat = fileForm.GetValues(fileKey);
                 pFile = Newtonsoft.Json.JsonConvert.DeserializeObject<Model.Patients.PatientFiles>(jsonformat[0]);
            }
            catch (Exception ex)
            {

                pFile.ErrorMessage = ex.ToString();
            }
            return Ok(pFile);
        }

但是iOS使用者得到了:

But the iOS guy got:

请求失败:不支持的媒体类型(415)

Request failed: unsupported media type (415)

推荐答案

在Web API控制器内部,您可以使用以下代码访问图像文件:-

Inside the web API controller you can access the image file using the code below :-

HttpPostedFile xmlFile = HttpContext.Current.Request.Files[0];

如果发布的文件不止一个,则将Files [0]分别替换为计数1或2等.

If you have more than one files posted, replace Files[0] with respective count 1 or 2 etc.

然后您可以使用以下代码访问JSON:

And then you can access the JSON using the code :

var fileForm = HttpContext.Current.Request.Form;
var fileKey = HttpContext.Current.Request.Form.Keys[0];
string[] jsonformat = fileForm.GetValues(fileKey);
var yourModel = JsonConvert.DeserializeObject<YourClassType>(jsonformat[0]);

如果您发布了多个json字符串,请分别用计数1或2等替换jsonformat [0].

If you have more than one json strings posted, replace jsonformat[0] with respective count 1 or 2 etc.

这篇关于如何在ASP.NET C#上接收MultipartFormData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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