gpt4 book ai didi

asp.net-mvc - Request.Content.ReadAsMultipartAsync -- 错误

转载 作者:行者123 更新时间:2023-12-01 22:41:36 33 4
gpt4 key购买 nike

使用以下代码,我不断收到错误消息:无法将类型“System.Net.Http.MultipartFormDataStreamProvider”隐式转换为“System.Threading.Tasks.Task>”

MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider("c:\\tmp\\uploads");

//Error line
Task<IEnumerable<HttpContent>> bodyparts = await Request.Content.ReadAsMultipartAsync(streamProvider);

我认为这很简单,但我想念它。

最佳答案

我最终使用了它并且效果很好。我在这里找到了

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);
}
}

关于asp.net-mvc - Request.Content.ReadAsMultipartAsync -- 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12586590/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com