使用web api读取multipartformdatacontent [英] Read multipartformdatacontent using web api
本文介绍了使用web api读取multipartformdatacontent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨伙计们
这是我第一次使用
hi guys
this is my first time using a
MultipartFormDataContent
i知道一些基本的如何使用它,但问题
是我不知道如何从API读取它一旦发送。
please检查下面的代码不完整,因为即时开始构建它。
我尝试过:
i know some basic how to use it but the problem
is i don't know how to read it from API once sent.
please check my code below not complete because im starting to build it.
What I have tried:
<pre lang="c#">
var fileStream = File.Open(file, FileMode.Open);
var fileInfo = new FileInfo(file);
var content = new MultipartFormDataContent();
content.Add(new StreamContent(fileStream), "\"file\"", string.Format("\"{0}\"", fileInfo.Name));
content.Add(new StringContent("some text"), "textname");
content.Add(new StringContent("some text2"), "textname2");
说明:
第一内容:添加文件(流式传输)
第二和第三内容:一些文字
现在我的问题我怎么能从API读取。
提前谢谢。 :)
EXPLANATION:
First content: adding file (streamed)
second and 3rd content: some text
now my question how can i read it from API.
Thank you in advance. :)
推荐答案
在代码中查看注释/说明中的注释。
See comments within code for notes/clarifications.
[HttpPost]
// I use attribute based routing so thats what this attribute below is for
[Route("documents/upload")]
public async Task<IHttpActionResult> UploadDocument()
{
//Error checking here to require multi part for form uploads
if (!Request.Content.IsMimeMultipartContent())
{
return BadRequest(string.Format(@"Media type is unsupported for file upload"));
}
//In your API project you should have a /Content directory, this is a directory under /Content called uploads. You can name whatever you want here as long as
// the directory exists
string root = HttpContext.Current.Server.MapPath("~/Content/Uploads");
System.IO.Directory.CreateDirectory(root);
var provider = new MultipartFormDataStreamProvider(root);
// This is how you would get any variables, other than your file, that came along with your upload. In your example you had textname and textname2. Below is how you
// could go about retrieving those values
var yourTextNameVariableInYourQuestion = provider.FormData.GetValues("textname").Select(m => m).FirstOrDefault();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.FileData)
{
FileInfo fileInfo = new FileInfo(file.LocalFileName);
if (fileInfo.Length != 0)
{
// Pretty self explanatory. I rename the file that is uploaded to something else, web api defaults the names to something like body_part****** so when I upload
// files in web api I go ahead and rename it to what the uploaded file name was.
string newFileName = file.LocalFileName +
Path.GetExtension(
file.Headers.ContentDisposition.FileName
.Replace("\"", string.Empty)
.Replace(@"\", string.Empty));
global::System.IO.File.Move(file.LocalFileName, newFileName);
}
}
// This has no error catching or anything so if something dies in the above, this will explode. Include proper error handling as you see fit
return Ok();
}
这篇关于使用web api读取multipartformdatacontent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文