标签上的图片怎么下载到本地

x1099389032 2016-10-11 02:34:17
现在我通过程序在<Img>标签上生成了一个二维码,并在<Img>标签下添加了一个a标签<a href='' style='text-align:center'>下载二维码</a>,现在想点击a标签的时候将这个二维码图片下载到本地。
做法是:A页面放<Img>和<a>标签,将<Img>标签的src指向B页面,B页面代码:
string url=Request.QueryString["url"];
Bitmap bmp = GetDimensionalCode(url);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.BinaryWrite(ms.GetBuffer());
尝试了几种方式不行,只能来求救了
...全文
1212 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
insus 2016-10-12
  • 打赏
  • 举报
回复
mirrorspace 2016-10-12
  • 打赏
  • 举报
回复
浏览器上看到这个IMG就说明已经到浏览器本机上了 如果需要在生成这个IMG的同时,在服务器上也保留一份,可以在response之前,保存到本地文件
跳动de指尖 2016-10-12
  • 打赏
  • 举报
回复
至于使用a标签产生用户下载的动作,可以使用html5的新属性

<a href="xxx.jpeg" download="下载时的文件名" >下载</a>
不过很可惜,目前只有chrome和firefox支持 图片文件,a标签再怎样,也时会被浏览器打开的,除非服务端向浏览器响应下载的指令
跳动de指尖 2016-10-12
  • 打赏
  • 举报
回复
当你的浏览器访问到这个页面的时候,并且显示了这张图片,证明这个图片已经被下载下来了 。
x1099389032 2016-10-11
  • 打赏
  • 举报
回复
没有filePath怎么办?需要先将生成的图片保存到服务器文件夹里吗,我<img>标签展示的图片是直接用生成的Response.BinaryWrite(ms.GetBuffer()); 没有保存起来
ttg520 2016-10-11
  • 打赏
  • 举报
回复
/// <summary>
        /// 输出流文件
        /// </summary>
        /// <param name="filePath"></param>
        public static void ReturnHTTPStream(string filePath)
        {
            FileInfo fi = new FileInfo(filePath);
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(fi.Name));
            HttpContext.Current.Response.AddHeader("Content-Length", fi.Length.ToString());
            HttpContext.Current.Response.AppendHeader("Last-Modified", fi.LastWriteTime.ToFileTime().ToString());
            HttpContext.Current.Response.AppendHeader("Location", HttpContext.Current.Request.Url.AbsoluteUri);
            HttpContext.Current.Response.ContentType = GetResponseContentType(fi.Extension);
            HttpContext.Current.Response.WriteFile(filePath);
            HttpContext.Current.Response.End();

        }

        /// <summary>
        /// Handle the Document 
        /// </summary>
        /// <param name="fileType"></param>
        /// <returns></returns>
        private static string GetResponseContentType(string fileType)
        {
            string result;
            switch (fileType.ToLower())
            {
                case ".doc": result = "application/msword"; break;
                case ".xls": result = "application/msexcel"; break;
                case ".txt": result = "text/plain"; break;
                case ".pdf": result = "application/pdf"; break;
                case ".psd": result = "application/pad"; break;
                case ".ppt": result = "appication/powerpoint"; break;
                default: result = "application/unknown"; break;
            }
            return result;
        }

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧