本文介绍了如何设置的WebAPI控制器的multipart / form-data的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何得到这个工作。我没有得到任何有用的错误信息与我的code,所以我用别的东西来产生的东西。我附上错误消息后code。我发现了一个上,但我不知道怎么跟我有什么实现它。这是我目前有

 公共异步任务<对象> PostFile()
    {
        如果(!Request.Content.IsMimeMultipartContent())
            抛出新的异常();
        VAR提供商=新MultipartMemoryStreamProvider();
        VAR的结果=新{文件=新的List<对象>()};
        VAR项目=新的文件();        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 [文件类型];        VAR经理=新的UserManager< ApplicationUser>(新UserStore< ApplicationUser>(新ApplicationDbContext()));
        变种用户= manager.FindById(User.Identity.GetUserId());        item.FileUploadedBy = user.Name;
        item.FileUploadDate = DateTime.Now;        等待Request.Content.ReadAsMultipartAsync(供应商)
         .ContinueWith(异步(一)= GT;
         {
             的foreach(在provider.Contents var文件)
             {
                 如果(file.Headers.ContentLength> 1000)
                 {
                     变种文件名= file.Headers.ContentDisposition.FileName.Trim('\\');
                     变种的contentType = file.Headers.ContentType.ToString();
                     。等待file.ReadAsByteArrayAsync()ContinueWith(B = GT; {item.FilePdf = b.Result;});
                 }
             }
         })展开()。        db.Files.Add(项目);
        db.SaveChanges();
        返回结果;    }

错误

Code used to generate error message

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

class

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; }
}
解决方案

I normally use the HttpPostedFileBase parameter only in Mvc Controllers. When dealing with ApiControllers try checking the HttpContext.Current.Request.Files property for incoming files instead:

[HttpPost]
public string UploadFile()
{
    var file = HttpContext.Current.Request.Files.Count > 0 ?
        HttpContext.Current.Request.Files[0] : null;

    if (file != null && file.ContentLength > 0)
    {
        var fileName = Path.GetFileName(file.FileName);

        var path = Path.Combine(
            HttpContext.Current.Server.MapPath("~/uploads"),
            fileName
        );

        file.SaveAs(path);
    }

    return file != null ? "/uploads/" + file.FileName : null;
}

这篇关于如何设置的WebAPI控制器的multipart / form-data的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:26