您的位置:首页 > 其它

上传文件 | 下载文件

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





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: