我想弄清楚如何完成这件事。我没有收到任何有用的错误消息,因此我使用了其他的东西来生成一些东西。我在错误消息后附加了该代码。我发现了一个tutorial,但我不知道如何使用我的东西来实现它。这是我目前有如何设置multipart/form-data的webapi控制器
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;
}
错误
对象{消息: “请求实体的媒体类型 '的multipart/form-data的' 不支持这个资源。”,exceptionMessage:“不MediaTypeFormatter可用于读取媒体类型为'multipart/form-data'的对象内容。“,exceptionType:”System.Net.Http.UnsupportedMediaTypeException“,stackTrace:”at System.Net.Http.HttpContentExtensions.ReadAs ... atterLogger ,CancellationToken cancellationToken)“} exceptionMessage:”没有MediaTypeFormatter可用于从媒体类型为'multipart/form-data'的内容中读取'HttpPostedFileBase'类型的对象。“exc eptionType:“System.Net.Http.UnsupportedMediaTypeException”消息:“请求实体的媒体类型'multipart/form-data'不支持此资源。”stackTrace:“at System.Net.Http.HttpContentExtensions.ReadAsAsync [T] (HttpContent内容,类型类型,IEnumerable的
1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken) ↵ at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable
1格式化器,IFormatterLogger formatterLogger,的CancellationToken的CancellationToken)
代码用于生成错误消息
[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; }
}
因此,它看起来像你从一个后端到另一个POST-ING的数据。你使用multipart/form而不是JSON或原始数据的原因是什么? – timothyclifford 2015-02-06 16:10:33
没有理由,这是我后来的工作。我张贴我的角度。我打开建议 – texas697 2015-02-06 16:13:57