HTML <input type="file">上传文件——结合asp.net的一个文件上传示例
2015-05-27 17:11
906 查看
HTML的代码:(关键是要在form里设置enctype="multipart/form-data",这样才能在提交表单时,将文件以二进制流的形式传输到服务器)
一、
浏览器中查看信息--
响应头信息 原始头信息
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" 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 = 0; i < Request.Files.Count; i++) { HttpPostedFile PostedFile = Request.Files[i]; if (PostedFile.ContentLength > 0) { string FileName = PostedFile.FileName;//此处文件名可能上传的是 客户端完成路径 C:\\123.png string strExPrentFile = FileName.Substring(FileName.LastIndexOf(".") + 1); string sFilePath = Server.MapPath("~/") + FileName;// Server.MapPath("~/123." + strExPrentFile); PostedFile.SaveAs(sFilePath); } else { //this.LabMessage.Text = "不能上传空文件"; } } Response.Write("1"); } } }
浏览器中查看信息--
响应头信息 原始头信息
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 = 0; i < Files.Count; i++) { HttpPostedFile PostedFile = Files[i]; if (PostedFile.ContentLength > 0) { string FileName = PostedFile.FileName; string strExPrentFile = FileName.Substring(FileName.LastIndexOf(".") + 1); string sFilePath = "/uploadfile/hotel/" + StringClass.makeFileName24() + i + strExPrentFile; PostedFile.SaveAs(Server.MapPath(sFilePath)); } else { //this.LabMessage.Text = "不能上传空文件"; } } }
相关文章推荐
- HTML <input type="file">结合asp.net的一个文件上传示例
- asp.net/c# 用<input type="file" />实现文件上传,multipart/form-data
- asp.net/c# 用<input type="file" />实现文件上传,multipart/form-data
- input file控件限制上传文件类型 网页上添加一个input file HTML控件: 1 <input id="File1" type="file" /> 默认是这样的,所有文件类
- ASP------<input type="file"/>上传文件
- 更改上传文件的样式<input type="file>
- 关于PHP HTML <input type="file" name="img"/>上传图片,图片大小,宽高,后缀名。
- 解决<input type=“file” multiple> 多文件上传问题(可以多次选取文件,也可以一次选取多个文件),而且点击的是一个同一个div
- 将通过<input type="file">上传的txt文件存储在localStorage,提取并构建File对象
- 使用<input type="file">实现文件上传
- <input type="file"/> 文件上传
- <input type="file">定义样式并获取上传文件路径及指定文件类型
- <input type="file">定义样式并获取上传文件路径及指定文件类型
- Android:让WebView支持<input type="file"…>元素,实现文件上传
- Asp.net网站中 js通过input type="file" 上传文件
- html的文件控件<input type="file">样式的改变
- input type="file" 上传文件,若是flv格式,则截取保存一张图片asp.net
- html中,文件上传时使用的<input type="file">的样式自定义
- 选择多个文件上传<input type="file" id="myfileinput" multiple>
- HTML+CSS优化“文件选择”控件(<input type="file">)