最近boss让写一个二维码的生成器,但是二维码生成后用户如果想下载二维码,这就促使我写l了 下载功能,小弟自认为技术不咋样,是个彻头彻尾的码农,本先是想用js来实现功能,但是查找了好多资料也没能实现,最后还是想到了后台代码。。。

好了废话就不多说了下面看代码吧....

  首先在前台页面加上一个

<asp:LinkButton runat="server" onclick="Unnamed1_Click">LinkButton</asp:LinkButton>

然后就是他的后台代码了

protected void Unnamed1_Click(object sender, EventArgs e)
{
string s_fileName = "/img/1.jpg"; //图片路径
HttpContext.Current.Response.ContentType = "application/ms-download"; //声明方式
string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName; 找到文件路径
System.IO.FileInfo file = new System.IO.FileInfo(s_path); //打开文件位置
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream"); //声明方式
HttpContext.Current.Response.Charset = "utf-8"; //声明方式
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
}

  

最新文章

  1. 2016HUAS_ACM暑假集训2D - 敌兵布阵
  2. [SQL]sql介绍
  3. Java 判断操作系统类型(适用于各种操作系统)
  4. C# 线程池异步调用
  5. 关于Update语句的锁
  6. linux daemon
  7. bzoj3289
  8. 如何javascript获取css中的样式
  9. [转]JAVA的动态代理机制及Spring的实现方式
  10. How to enable your website to public(set up your web server at home)
  11. JavaScript语法基础:数组的常用方法详解
  12. DH密钥交换非对称加密
  13. nmap的使用
  14. webpack浅析---出口篇
  15. sass重构响应式unofficial‘s博客轻松适应移动端
  16. mysql更新字段值提示You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode
  17. 最大的矩形面积 Maximal Rectangle
  18. ThreadLocal 详解
  19. Window.sessionStorage
  20. FFmpeg Basic学习笔记(4)

热门文章

  1. linux 查看当前路径命令:pwd
  2. cocos2d-x 多线程以及线程同步
  3. 手机调用系统的拍照和裁剪功能,假设界面有输入框EditText,在一些手机会出现点击EditText会弹出输入法,却不能输入的情况。
  4. 【转】web测试内容及工具经典总结
  5. Java网页数据采集器[中篇-数据存储]【转载】
  6. Codeforces Round #332 (Div. 2) C. Day at the Beach 线段树
  7. Codeforces Gym 100733A Shit&#225;lia 计算几何
  8. 在 C# 中加载自己编写的动态链接库
  9. 【读jQuery源码有感系列一】callee
  10. 实例源码--Android时钟源码