不疯不成魔

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

文件的上传和下载

<body>
<form enctype="multipart/form-data" method="post" action="处理.ashx">
<input id="File1" type="file" name="FileUpload" />
<input id="Submit1" type="submit" value="提交" />
<a href="FileDownLoad.ashx">下载</a>
</form>
 /// </summary>
    public class 处理 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            HttpPostedFile file=context.Request.Files[0];
            file.SaveAs(context.Server.MapPath(Guid.NewGuid().ToString()+file.FileName));
            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

 /// </summary>
    public class FileDownLoad : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //url编码
          string endodeFileName= HttpUtility.UrlEncode("01 淡定英语怎么说.bhd");
            //添加一个响应头,指定以附件的形似
          context.Response.AppendHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", endodeFileName));
            //把文件的内容作为流输出
          context.Response.WriteFile("01 淡定英语怎么说.bhd");
           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

posted on 2014-07-27 09:38  不疯不成魔  阅读(151)  评论(0)    收藏  举报