如何获得的WebAPI控制器的文件名和文件大小? [英] how to get File Name and File Size in WebAPI controller?

查看:241
本文介绍了如何获得的WebAPI控制器的文件名和文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC 4和的WebAPI,需要使用文件上传控件上传图片。
我需要知道替换为的WebAPI以下内容:

I am using MVC 4 and webAPI and need to upload a image using file upload control. I need to know the replacement for the following in 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控制器的文件名和文件大小。

I mean I need to get the file name and file size in WebAPI controller.

Request.Files

推荐答案

您可以使用的 HttpPostedFile类

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

这篇关于如何获得的WebAPI控制器的文件名和文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆