HTML的代码:(关键是要在form里设置enctype="multipart/form-data",这样才能在提交表单时,将文件以二进制流的形式传输到服务器)

一、

<form id="form1" action="test.aspx" method="post" enctype="multipart/form-data">
<div>
<input type="file" name="fl" />
<input type="submit" name="sb" value="submit" />
</div>
</form>
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = ; i < Request.Files.Count; i++)
{
HttpPostedFile PostedFile = Request.Files[i];
if (PostedFile.ContentLength > )
{
string FileName = PostedFile.FileName;//此处文件名可能上传的是 客户端完成路径 C:\\123.png
string strExPrentFile = FileName.Substring(FileName.LastIndexOf(".") + ); string sFilePath = Server.MapPath("~/") + FileName;// Server.MapPath("~/123." + strExPrentFile);
PostedFile.SaveAs(sFilePath);
}
else
{
//this.LabMessage.Text = "不能上传空文件";
}
}
Response.Write("");
}
}
}

浏览器中查看信息--

响应头信息   原始头信息
Cache-Control    
private
Connection    
Close
Content-Length    
458
Content-Type    
text/html; charset=utf-8
Date    
Thu, 28 May 2015 03:06:47 GMT
Server    
ASP.NET Development Server/10.0.0.0
X-AspNet-Version    
4.0.30319
请求头信息   原始头信息
Accept    
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding    
gzip, deflate
Accept-Language    
zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Connection    
keep-alive
Cookie    
oa_cookiename=%e8%82%96%e5%b7%a5; oa_cookieId=291; 肖工_log=operation=&TlogGUI=ed1624e4-9e1d-44fb-98f7-1298512a8266
; ASP.NET_SessionId=cfufw4sesznnphj4peooed0d; SLnewses=1; WPTLNG=1
Host    
localhost:13771
Referer    
http://localhost:13771/AppWeb/test.aspx
User-Agent    
Mozilla/5.0 (Windows NT 6.1; rv:38.0) Gecko/20100101 Firefox/38.0

来自上传流的请求头信息  
Content-Length    
430
Content-Type    
multipart/form-data; boundary=---------------------------18319207917180

POST
部分multipart/form-data
fl    
‰PNG

���
IHDR���������
���bKGD�ÿ�ÿ�ÿ ½§“���    pHYs��Ä��ĕ+���'IDAT™Á    � °>ó/I6º’ðž×,·¢¬’eEòàXŒòmY����IEND®B

sb    
submit
源代码
-----------------------------18319207917180
Content-Disposition: form-data; name="fl"; filename="QQ20150528110621.png"
Content-Type: image/png

‰PNG

���
IHDR���������
���bKGD�ÿ�ÿ�ÿ ½§“���    pHYs��Ä��ĕ+���'IDAT™Á    � °>ó/I6º’ðž×,·¢¬’eEòàXŒòmY����IEND®B

-----------------------------18319207917180
Content-Disposition: form-data; name="sb"

submit
-----------------------------18319207917180--

二、

<form id="form1" runat="server"  method="post" enctype="multipart/form-data">
<div>
<input type="file" />
<asp:Button ID="btnUpload" runat="server" Text="开始上传" onclick="btnUpload_Click" />
</div>
</form>
protected void btnUpload_Click(object sender, EventArgs e)
{
//int intCount = RequestClass.GetFormInt("hdcount", -1);
HttpFileCollection Files = HttpContext.Current.Request.Files;
for (int i = ; i < Files.Count; i++)
{
HttpPostedFile PostedFile = Files[i];
if (PostedFile.ContentLength > )
{
string FileName = PostedFile.FileName;
string strExPrentFile = FileName.Substring(FileName.LastIndexOf(".") + ); string sFilePath = "/uploadfile/hotel/" + StringClass.makeFileName24() + i + strExPrentFile;
PostedFile.SaveAs(Server.MapPath(sFilePath));
}
else
{
//this.LabMessage.Text = "不能上传空文件";
}
}
}

HTML <input type="file">上传文件——结合asp.net的一个文件上传示例的更多相关文章

  1. input[type='file']样式美化及实现图片预览

    前言 上传图片是常见的需求,多使用input标签.本文主要介绍 input标签的样式美化 和 实现图片预览. 用到的知识点有: 1.input标签的使用 2.filelist对象 和 file对象 3 ...

  2. input type=file 选择图片并且实现预览效果的实例

    为大家带来一篇input type=file 选择图片并且实现预览效果的实例. 通过<input />标签,给它指定type类型为file,可提供文件上传: accept:可选择上传类型, ...

  3. input type=file 选择图片并且实现预览效果

    通过<input />标签,给它指定type类型为file,可提供文件上传: accept:可选择上传类型,如:只要传图片,且不限制图片格式,为image/*: multiple:规定是否 ...

  4. input type="file"使用

    问题: 在实际开发过程中,会遇到上传文件的一些需求.但是使用原生的<input type="file" />在使用中存在一些问题 在未上传文件时,显示"no ...

  5. 原来样式改变不了(input type="file")

    label { background-color: #979fa8; color: #fff; display: inline-block; padding: .8rem 4rem; cursor: ...

  6. html中,文件上传时使用的<input type="file">的样式自定义

    Web页面中,在需要上传文件时基本都会用到<input type="file">元素,它的默认样式: chrome下: IE下: 不管是上面哪种,样式都比较简单,和很多 ...

  7. 上传文件 隐藏input type="file",用text显示

    <div> <span>上传文件:</span> <input type="file" id="upload_file" ...

  8. javascript input type=file 文件上传

    在JS中,input type=file 是常用的文件上传API,但感觉W3C说的不是很清楚,同时网上的资料也比较乱. 由于做微信开发,所以网页打算尽量少用第三方库或者插件,以加快网页的加载速度.因为 ...

  9. 用jQuery重置用于文件上传的input (type="file")

    页面中有如下标签: <input type="file" id="upload"/> 此标签本用于文件上传,现在有需要将其值重置为空.于是想当然地写 ...

随机推荐

  1. Resharper 检测所有NullReferenceExceptions(空指针)

    需求分析 "null exception"很见的一种异常,但在某些情况下却会引起严重的bug! 本文目的就是对代码进行null 的检查,避免不应该出现的Error. 本文借助res ...

  2. TCP/IP协议(二)tcp/ip基础知识

    今天凌晨时候看书,突然想到一个问题:怎样做到持续学习?然后得出这样一个结论:放弃不必要的社交,控制欲望,克服懒惰... 然后又有了新的问题:学习效率时高时低,状态不好怎么解决?这也是我最近在思考的问题 ...

  3. Unity中脚本的执行顺序总结(@WhiteTaken)

    (Editor)以上是Unity官方文档中的截图,脚本在被挂载到物体上,会启用Editor的方法Reset. (Initialization)当执行脚本开始,初始化的过程中,依次执行的是Awake-& ...

  4. [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...

  5. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. 【原】Learning Spark (Python版) 学习笔记(四)----Spark Sreaming与MLlib机器学习

    本来这篇是准备5.15更的,但是上周一直在忙签证和工作的事,没时间就推迟了,现在终于有时间来写写Learning Spark最后一部分内容了. 第10-11 章主要讲的是Spark Streaming ...

  7. ElasticSearch第三步-中文分词

      ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticS ...

  8. 自动判断应该Ajax还是return

    起因 最近回顾以前的代码,发现一个偶尔会见到的现象.一个类里面的方法可能需要Ajax返回,也有可能需要函数return.这个现象发生在网站MVC中的 逻辑层(或模型层),示例如下.IndexCtrl是 ...

  9. 昆仑游戏[JS加密修改]

    昆仑游戏:http://www.kunlun.com/index.html JS加密修改 BigTools=window.BigTools;//重点 RSAKeyPair=window.RSAKeyP ...

  10. JavaScript简单对象的定义方法

    工厂模式: 初级开发者可能会这样定义对象: var obj = new Object(); obj.name = "hero"; obj.showName=function (){ ...