【问题标题】:how to get File Name and File Size in WebAPI controller?如何在 WebAPI 控制器中获取文件名和文件大小?
【发布时间】:2014-01-30 14:48:20
【问题描述】:

我正在使用 MVC 4 和 webAPI,需要使用文件上传控件上传图像。 我需要知道 WebAPI 中以下内容的替换:

int filelength = Request.Files[0].ContentLength;
byte[] databytes = new byte[filelength];
Request.Files[0].InputStream.Read(databytes, 0, filelength);
string filename = Path.GetFileName(Request.Files[0].FileName);

我的意思是我需要在 WebAPI 控制器中获取文件名和文件大小。

Request.Files 的任何替代方案?

【问题讨论】:

    标签: asp.net asp.net-mvc-4


    【解决方案1】:

    您可以使用HttpPostedFile class

    HttpPostedFile file = HttpContext.Current.Request.Files[0];
    
        if (file.ContentLength > 0) // file.ContentLength is the file size in bytes
        {
          var fileName = System.IO.Path.GetFileName(file.FileName);    
        }
    

    【讨论】:

      猜你喜欢
      • 2014-03-25
      • 1970-01-01
      • 2021-07-26
      • 2012-05-29
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多