小弟初学,想请教.net 中根据url下载图片,页面还要有文件保存的弹出窗

Ming_Sky1 2018-01-11 12:34:28
各位大神,小弟初学,被这个东西弄的。。。
我也百度过一些不过都是那种直接保存的没有弹出窗的
想要实现一个点击按钮根据url去下载图片,要有文件保存框的提示类似这样的
应该怎么做,希望能有源码
url就是类似这样的 http://www.csdn.net/images/logo_csdn.gif
来这请教各位大神
...全文
400 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ming_Sky1 2018-01-11
  • 打赏
  • 举报
回复
好像有点问题,就是下载的图片好像不完整,gif下载好像也没用
Ming_Sky1 2018-01-11
  • 打赏
  • 举报
回复
暂时解决了,后来我是把这个网络流转成内存流之后下载的,也不清楚这里面有没有一些不当之处 代码有些是copy的,所以谅解 string url1 = "http://pic4.nipic.com/20091217/3885730_124701000519_2.jpg"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1); request.Method = "GET"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.OK) { Stream rs = response.GetResponseStream(); //网络流转换为内存流 var ms = StreamToMemoryStream(rs); ms.Seek(0, SeekOrigin.Begin); int buffsize = (int)ms.Length; //rs.Length 此流不支持查找,先转为MemoryStream byte[] bytes = new byte[buffsize]; ms.Read(bytes, 0, buffsize); ms.Flush(); ms.Close(); rs.Flush(); rs.Close(); //以文件流的方式下载 Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } } private MemoryStream StreamToMemoryStream(Stream instream) { //将网络流转换成内存流 MemoryStream outstream = new MemoryStream(); const int bufferLen = 4096; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = instream.Read(buffer, 0, bufferLen)) > 0) { outstream.Write(buffer, 0, count); } return outstream; }
Ming_Sky1 2018-01-11
  • 打赏
  • 举报
回复
引用 6 楼 xuzuning 的回复:
你是在做什么应用?
如果是 web 应用,那么当浏览器遇到了不能解释成页面内容的类型声明时,就会弹出下载对话框
如果你想让浏览器对能解析的文件类型也产生下载框,那么可附加 Content-type: application/force-download、Content-type: application/octet-stream 这样的类型声明

如果是 桌面应用,可在获取远程数据成功之后,自己打开保存对话框

这是我写的代码,因为不太懂,也不知道对不对,因为我测试过后,能下载,也有弹出另存为的窗口,但是下载下来的图片就会是已损坏,

string url1 = "http://pic4.nipic.com/20091217/3885730_124701000519_2.jpg";
HttpWebRequest wreq = HttpWebRequest.Create(url1) as HttpWebRequest;
HttpWebResponse response = wreq.GetResponse() as HttpWebResponse;
MemoryStream ms = null;
using (var stream = response.GetResponseStream())
{
Byte[] buffer = new Byte[response.ContentLength];
int offset = 0, actuallyRead = 0;
do
{
actuallyRead = stream.Read(buffer, offset, buffer.Length - offset);
offset += actuallyRead;
}
while (actuallyRead > 0);
ms = new MemoryStream(buffer);
}
byte[] content = ms.ToArray();
var c1 = 1;
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=2.jpg");
Response.BinaryWrite(content);
Response.Flush();
Response.End();
因为对这个不太懂,基本都是copy来的.........
xuzuning 2018-01-11
  • 打赏
  • 举报
回复
你是在做什么应用? 如果是 web 应用,那么当浏览器遇到了不能解释成页面内容的类型声明时,就会弹出下载对话框 如果你想让浏览器对能解析的文件类型也产生下载框,那么可附加 Content-type: application/force-download、Content-type: application/octet-stream 这样的类型声明 如果是 桌面应用,可在获取远程数据成功之后,自己打开保存对话框
  • 打赏
  • 举报
回复
http://blog.csdn.net/starfd/article/details/45840853 网上图片下载可以参考下
  • 打赏
  • 举报
回复
File方法有好几个方法,从网上下载图片的话,就是模拟请求,然后将Response的结果以byte[]传给file方法
Ming_Sky1 2018-01-11
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
        public ActionResult DownLoad()
        {
            return File(Server.MapPath("~/Desert.jpg"), "application/octet-stream", "Desert.jpg");
        }
这是MVC下如何返回,webform你可以查下怎么输出验证码图片,关键就是指定输出application/octet-stream
因为这里的图片是http://www.csdn.net/images/logo_csdn.gif,这里只是做个例子,并不是验证码之类,就是各种图片,你这里的server.MapPath()的话应该是本地服务器上的图片吧?我想要访问网络上的图片然后下载的
  • 打赏
  • 举报
回复
文件保存框这是浏览器的事情,貌似现在高版本都是直接下载到默认目录,提示都不给的
  • 打赏
  • 举报
回复
        public ActionResult DownLoad()
        {
            return File(Server.MapPath("~/Desert.jpg"), "application/octet-stream", "Desert.jpg");
        }
这是MVC下如何返回,webform你可以查下怎么输出验证码图片,关键就是指定输出application/octet-stream

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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