HttpPostedFile类 HttpPostedFile转字节bate
2016-01-06 11:51
796 查看
在研究HttpRequest的时候,搞文件上传的时候,经常碰到返回HttpPostedFile对象的情况,这个对象才是真正包含文件内容的东西。
经常要获取的最重要的内容是FileName属性与SavaAs方法,现在我们来详细看看这个东西。
一、常用属性
二、常用方法
代码示例:
注意表单要加上enctype = “multipart/form-data”,后台FileCollect.Count才不会为0。如:
public ActionResult GetForm()
{
HttpRequest request = System.Web.HttpContext.Current.Request;
HttpFileCollection FileCollect = request.Files;
if (FileCollect.Count > 0) //如果集合的数量大于0
{
foreach (string str in FileCollect)
{
HttpPostedFile FileSave = FileCollect[str]; //用key获取单个文件对象HttpPostedFile
string imgName = DateTime.Now.ToString(“yyyyMMddhhmmss”);
string imgPath = “/” + imgName + FileSave.FileName; //通过此对象获取文件名
string AbsolutePath = Server.MapPath(imgPath);
FileSave.SaveAs(AbsolutePath); //将上传的东西保存
Response.Write(“
”);
}
}
return Content(“键值对数目:” + FileCollect.Count);
}
经常要获取的最重要的内容是FileName属性与SavaAs方法,现在我们来详细看看这个东西。
一、常用属性
ContentLength: 获取上载文件的大小(以字节为单位)。返回一个数字。 ContentType:获取客户端发送的文件的 MIME 内容类型。 FileName: 获取客户端上的文件的完全限定名称。 InputStream:获取一个 Stream 对象,该对象指向一个上载文件,以准备读取该文件的内容。
二、常用方法
SaveAs 保存上载文件的内容。 可以服务器物理路径作为参数。
代码示例:
注意表单要加上enctype = “multipart/form-data”,后台FileCollect.Count才不会为0。如:
public ActionResult GetForm()
{
HttpRequest request = System.Web.HttpContext.Current.Request;
HttpFileCollection FileCollect = request.Files;
if (FileCollect.Count > 0) //如果集合的数量大于0
{
foreach (string str in FileCollect)
{
HttpPostedFile FileSave = FileCollect[str]; //用key获取单个文件对象HttpPostedFile
string imgName = DateTime.Now.ToString(“yyyyMMddhhmmss”);
string imgPath = “/” + imgName + FileSave.FileName; //通过此对象获取文件名
string AbsolutePath = Server.MapPath(imgPath);
FileSave.SaveAs(AbsolutePath); //将上传的东西保存
Response.Write(“
”);
}
}
return Content(“键值对数目:” + FileCollect.Count);
}
HttpPostedFile img = files.Get(0); byte[] bytes = new byte[img.ContentLength]; using (BinaryReader reader = new BinaryReader(img.InputStream, Encoding.UTF8)) { bytes = reader.ReadBytes(0) }
相关文章推荐
- HttpPostedFile类
- $_SERVER['HTTP_HOST']
- Web容器自动对HTTP请求中参数进行URLDecode处理
- TCP连接探测中的Keepalive 和心跳包
- 在Linux下,在网络没有配置好前,怎样查看网卡的MAC地址?
- Android OkHttp完全解析
- linux上网络配置不生效现象处理
- http请求417错误
- 深入理解HTTP Session
- Bacula网络备份环境搭建
- HttpClients 如何自动处理重定向
- python中http请求方法库汇总
- C#网络编程之同步异步
- 元旦闲得:网络流量的经济属性浅解
- Namespace:Openstack的网络实现
- Python 写网络爬虫思路分析
- httpclient 调用WebAPI
- mac 网络共享 wifi共享
- python网络编程之SimpleServer/Client
- Linux下的wget和curl如何使用http proxy