【发布时间】:2015-04-06 19:36:46
【问题描述】:
我正在努力解决这个问题。我的代码没有收到任何有用的错误消息,所以我使用了其他东西来生成一些东西。我在错误消息后附上了该代码。我在上面找到了tutorial,但我不知道如何用我所拥有的来实现它。这是我目前拥有的:
public async Task<object> PostFile()
{
if (!Request.Content.IsMimeMultipartContent())
throw new Exception();
var provider = new MultipartMemoryStreamProvider();
var result = new { file = new List<object>() };
var item = new File();
item.CompanyName = HttpContext.Current.Request.Form["companyName"];
item.FileDate = HttpContext.Current.Request.Form["fileDate"];
item.FileLocation = HttpContext.Current.Request.Form["fileLocation"];
item.FilePlant = HttpContext.Current.Request.Form["filePlant"];
item.FileTerm = HttpContext.Current.Request.Form["fileTerm"];
item.FileType = HttpContext.Current.Request.Form["fileType"];
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var user = manager.FindById(User.Identity.GetUserId());
item.FileUploadedBy = user.Name;
item.FileUploadDate = DateTime.Now;
await Request.Content.ReadAsMultipartAsync(provider)
.ContinueWith(async (a) =>
{
foreach (var file in provider.Contents)
{
if (file.Headers.ContentLength > 1000)
{
var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
var contentType = file.Headers.ContentType.ToString();
await file.ReadAsByteArrayAsync().ContinueWith(b => { item.FilePdf = b.Result; });
}
}
}).Unwrap();
db.Files.Add(item);
db.SaveChanges();
return result;
}
错误:
Object {message: "该资源不支持请求实体的媒体类型'multipart/form-data'。", exceptionMessage: "没有 MediaTypeFormatter 可用于读取媒体类型为'multipart/form 的对象...om 内容-data'.", exceptionType: "System.Net.Http.UnsupportedMediaTypeException", stackTrace: " at System.Net.Http.HttpContentExtensions.ReadAs...atterLogger, CancellationToken cancelToken)"}exceptionMessage: "没有 MediaTypeFormatter 可用于读取对象来自媒体类型为“multipart/form-data”的内容的“HttpPostedFileBase”类型。”exceptionType:“System.Net.Http.UnsupportedMediaTypeException”消息:“不支持请求实体的媒体类型“multipart/form-data” resource."stackTrace:" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable
1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken) ↵ at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancelToken)
用于生成错误信息的代码:
[HttpPost]
public string UploadFile(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(HttpContext.Current.Server.MapPath("~/uploads"), fileName);
file.SaveAs(path);
}
return "/uploads/" + file.FileName;
}
类:
public class File
{
public int FileId { get; set; }
public string FileType { get; set; }
public string FileDate { get; set; }
public byte[] FilePdf { get; set; }
public string FileLocation { get; set; }
public string FilePlant { get; set; }
public string FileTerm { get; set; }
public DateTime? FileUploadDate { get; set; }
public string FileUploadedBy { get; set; }
public string CompanyName { get; set; }
public virtual ApplicationUser User { get; set; }
}
【问题讨论】:
-
所以看起来您正在将数据从一个后端发布到另一个后端。您使用多部分/表单而不是 JSON 或原始数据的任何原因?
-
没有理由,这对我有用。我发布了我的角度。我欢迎建议
-
@timothyclifford 为什么不呢? Multipart 是一种有效的方法。此 QA 页面的重点不是建议另一种方法,而是使用您正在寻找的方法解决您遇到的问题,
标签: c# asp.net-web-api multipartform-data