[HttpPost]
[Route("Profile")]
public async Task<IHttpActionResult> SubmitPhotos()
{
// Check if the request contains multipart/form-data. // shivam agarwal
if (!Request.Content.IsMimeMultipartContent())
{
ModelState.AddModelError("MIME not supported", "MIME not supported");
return BadRequest();
}
string root = HttpContext.Current.Server.MapPath("~/Content/ProfileImages/");
var provider = new MultipartFormDataStreamProvider(root);
try
{
var newResponse = await Request.Content.ReadAsMultipartAsync(provider);
// pleae check here
//provider=new FormDataCollection(Request.Content);
if (provider.FormData.GetValues("UserId").First() == null || provider.FormData.GetValues("UserId").First() == "")
{
return BadRequest("UserId is required");
}
else if (provider.FormData.GetValues("DisplayName").First() == null || provider.FormData.GetValues("DisplayName").First() == "")
{
return BadRequest("DisplayName is required");
}
foreach (MultipartFileData fileData in provider.FileData)
{
// Image saving process
model.ImageKey = keyvalue[0];
// call business function
if (!response.Exceptions.Any())
{
//saving to database
return Ok(resultResponse);
}
}
return Ok();
}
catch (System.Exception e)
{
ModelState.AddModelError("Exception", e.Message);
return BadRequest();
}
finally
{
FlushTempFiles();
}
}
我正在使用webapi将表单集合值提交到数据库。主要输入是文件(Photo),Displayname,UserID 从移动应用程序用户提交三个输入,它将图像保存到文件夹中,其他两个值输入数据库。到现在为止一切都很好。
我的问题是:在我的情况下文件输入是可选的,所以真实的是它们可能会也可能不会上传文件输入(Filedata)
所以,我在这一行中遇到错误
var newResponse = await Request.Content.ReadAsMultipartAsync(provider);
MIME多部分流的意外结束。 MIME多部分消息不完整。
我已经阅读了将\ r \ n添加到流中的答案,但在我的情况下它不起作用。请告诉我您的意见我如何处理可选文件输入并获取用户ID 和 displayname 的值?
答案 0 :(得分:1)
我认为您只需要检查您的移动客户端应用。如果您的移动应用是Android应用,那么您可以参考以下示例代码:
如果使用OkHttp:
$this->db->select('*');
//$this->db->from('shop'); <-- no need of this line
$this->db->join('city', 'city.city_id = shop.city_id');
$this->db->where('city', array('city.city_name' => 'Bangalore'));
$query = $this->db->get("shop");//<-- give table name here only.
如果使用Volley,你可以参考My GitHub sample,你应该注意
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addPart(
Headers.of("Content-Disposition", "form-data; name=\"title\""),
RequestBody.create(null, "Square Logo"))
// .addPart(
// Headers.of("Content-Disposition", "form-data; name=\"file\"; filename=\"myfilename.png\""),
// RequestBody.create(MEDIA_TYPE_PNG, bitmapdata))
.build();
final Request request = new Request.Builder()
.url("http://myserver/api/files")
.post(requestBody)
.build();
我在Web API的调试屏幕上,您可以看到FileData.Count为0:
<强>更新强>
IMO,您还应该阅读以下内容:
Sending HTML Form Data in ASP.NET Web API: File Upload and Multipart MIME
在那里,你会发现:
通过查看示例请求,最容易理解多部分MIME邮件的格式:
// send multipart form data necesssary after file data
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
因此,如果您的客户端应用程序未发送最后一个POST http://localhost:50460/api/values/1 HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------41184676334
Content-Length: 29278
-----------------------------41184676334
Content-Disposition: form-data; name="caption"
Summer vacation
-----------------------------41184676334
Content-Disposition: form-data; name="image1"; filename="GrandCanyon.jpg"
Content-Type: image/jpeg
(Binary data not shown)
-----------------------------41184676334--
,那么您的Web API将抛出-----------------------------41184676334--