我想弄清楚如何完成這件事。我沒有收到任何有用的錯誤消息,因此我使用了其他的東西來生成一些東西。我在錯誤消息後附加了該代碼。我發現了一個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