2014-01-30 16 views
0

我使用的是MVC 4和webAPI,需要使用文件上傳控件上傳圖片。 我需要知道更換中的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的任何替代方法?

回答

0

可以使用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);  
    } 
相關問題