Request.Content.ReadAsMultipartAsync - 错误 [英] Request.Content.ReadAsMultipartAsync -- Error
本文介绍了Request.Content.ReadAsMultipartAsync - 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过下面的code我不断收到错误:无法隐式转换类型'System.Net.Http.MultipartFormDataStreamProvider'到'System.Threading.Tasks.Task>
With the below code I keep getting the error: Cannot implicitly convert type 'System.Net.Http.MultipartFormDataStreamProvider' to 'System.Threading.Tasks.Task>'
MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider("c:\\tmp\\uploads");
//Error line
Task<IEnumerable<HttpContent>> bodyparts = await Request.Content.ReadAsMultipartAsync(streamProvider);
我觉得这是一个简单的,但我失踪了。
I think this is an easy one, but I'm missing it.
推荐答案
最后我用这个和它的伟大工程。我发现在这里
I ended up using this and it works great. I found it here
<一个href=\"http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2\">http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
public class UploadController : ApiController
{
public async Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
}
这篇关于Request.Content.ReadAsMultipartAsync - 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文