上传文件 | 下载文件
2015-08-21 19:10
393 查看
上传文件
客户端:HtmlPage1.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <!--上传文件的时候: 1>必须使用Post方式来提交数据 2>必须设置enctype属性值为multipart/form-data,表示将数据以二进制的方式提交到服务器; 如果不设置的话enctype的默认值为application/x-www-form-urlencoded,表示将数据以键值对的方法是提交到服务器 --> <form action="Handler1.ashx" method="post" enctype="multipart/form-data"> <input type="file" name="fileName" value="" /> <input type="submit" value="上传"/> </form> </body> </html>
服务端:Handler1.ashx
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace 上传文件 { /// <summary> /// Handler1 的摘要说明 /// </summary> public class Handler1 : IHttpHandler { public void ProcessRequest(HttpContext context) { //设置报文头的content-type属性 context.Response.ContentType = "text/plain"; //获取浏览器发送过来的文件数据; HttpPostedFile file = context.Request.Files["fileName"]; //获取上传的文件名。 string fileName= file.FileName; //int fileLength = context.Request.ContentLength; 其实我们也可以获取文件的大小 //取一个新的名字(在旧文件名前加上一个Guid就是为了防止上传的文件重名,产生冲突) string newfileName = Guid.NewGuid().ToString() + "_" + fileName; //将这个文件保存到项目下的files文件夹中 file.SaveAs(context.Server.MapPath("files")+"/" + newfileName); context.Response.Write("OK"); } public bool IsReusable { get { return false; } } } }
下载文件
客户端:HtmlPage1.html<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form> <a href="Handler1.ashx?fileName=123.jpg">123.jpg</a><br/> <a href="Handler1.ashx?fileName=abc.txt">abc.txt</a><br /> <a href="Handler1.ashx?fileName=word.docx">word.docx</a><br /> <a href="Handler1.ashx?fileName=介绍信模板.xls">介绍信模板.xls</a><br /> </form> </body> </html>
服务端:Handler1.ashx
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace 下载文件 { /// <summary> /// Handler1 的摘要说明 /// </summary> public class Handler1 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //获取用户要下载的资源名称 string filename = context.Request["fileName"]; //添加报文头(请参考:http://blog.csdn.net/fanbin168/article/details/38535735) context.Response.AddHeader("Content-disposition", "attachment; filename=" +context.Server.UrlDecode( filename)); //获取到用户要下载的资源后,读取磁盘文件,把该文件发送给客户端 context.Response.WriteFile("~/Download/" + filename); } public bool IsReusable { get { return false; } } } }
相关文章推荐
- 基于网页可信特征的信息可信度评估方法(IEEE2011)
- 队列
- Access restriction: The method createJPEGEncoder(OutputStream) from the type JPEGCodec is not access
- 【LeetCode】(263)Ugly Number(Easy)
- BS_OWNERDRAW风格的作用和例子,值得研究~
- 洛谷1177 快速排序 解题报告
- FIREDAC FDConnection 连接池
- 利用ALSA插件将5.1通道音频通过2通道立体声卡播放的配置(.asoundrc配置文件)
- 杭电1045
- Ubuntu 14.04中升级gcc到版本4.9.2并切换使用它们
- day9: 内存管理初级:内存管理的方式、引用计数机制,影响计数的各个方法、dealloc方法、内存管理的基本原则
- 每天进步一点点——Linux文件锁编程flock
- form表单的简单验证onsubmit
- android 组件SeekBar实例
- 博弈
- iBatis调用存储过程的方法
- uva 725Division
- codeforces Gym 100338F Spam Filter 垃圾邮件过滤器(模拟,实现)
- 欢迎使用CSDN-markdown编辑器
- C++: std::string 与 Unicode 如何结合?