关于ASP.NET实现图片的下载

UKnowImJin 2017-09-11 04:46:08
求教大佬怎样下载其他网页上的图片,不是那种给一个固定图片链接的下载,
是根据据图片源命名的下载,不用给图片重命名的那种
...全文
709 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Veary 2017-09-13
  • 打赏
  • 举报
回复
引用 10 楼 chb345536638 的回复:
另存为的只有设置过的标头Content-Disposition值才会有的

            string oldName = string.Format("{0}.jpg",Guid.NewGuid());

            WebClient wc = new WebClient();

            wc.DownloadFile("图片路径", oldName);


            if (wc.ResponseHeaders != null)
            {
                string cpString = wc.ResponseHeaders["Content-Disposition"];

                if (!string.IsNullOrEmpty(cpString))
                {
                    ContentDisposition contentDisposition = new ContentDisposition(cpString);
                    string fileName = contentDisposition.FileName;

                    if (!File.Exists(fileName))
                    {
                        File.Move(oldName, fileName);
                    }
                    else
                    {
                        //
                    }
                }
            }
            
至于其他的比如,你要获取我的头像文件名,那只是字符串处理的事,写个正则就获取到了

[^/]*$
事实上我试验过那些没有Content-Disposition的图片,使用浏览器右键另存为他还是有自己的名字的。
  • 打赏
  • 举报
回复
引用 10 楼 chb345536638 的回复:
另存为的只有设置过的标头Content-Disposition值才会有的
不应该执行 wc.DownloadFile("图片路径", oldName),只要读取网站返回前面1k 字节就够了。
  • 打赏
  • 举报
回复
另存为的只有设置过的标头Content-Disposition值才会有的

            string oldName = string.Format("{0}.jpg",Guid.NewGuid());

            WebClient wc = new WebClient();

            wc.DownloadFile("图片路径", oldName);


            if (wc.ResponseHeaders != null)
            {
                string cpString = wc.ResponseHeaders["Content-Disposition"];

                if (!string.IsNullOrEmpty(cpString))
                {
                    ContentDisposition contentDisposition = new ContentDisposition(cpString);
                    string fileName = contentDisposition.FileName;

                    if (!File.Exists(fileName))
                    {
                        File.Move(oldName, fileName);
                    }
                    else
                    {
                        //
                    }
                }
            }
            
至于其他的比如,你要获取我的头像文件名,那只是字符串处理的事,写个正则就获取到了

[^/]*$
汉升软件 2017-09-12
  • 打赏
  • 举报
回复
string filePath = System.Configuration.ConfigurationManager.AppSettings["ImageUploadPath"] + fileurl; FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.Charset = "UTF-8"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); // Response.ContentType = "application/octet-stream"; Response.ContentType = "application/zip"; if (!string.IsNullOrEmpty(fileName)) { Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); } else { Response.AddHeader("Content-Disposition", "attachment; filename=" + fileurl); } Response.BinaryWrite(bytes); try { Response.Flush(); } catch { } Response.End();
Veary 2017-09-12
  • 打赏
  • 举报
回复
然而我的关注点已经转移到了已知一个图片链接地址,如何Get图片的文件名。
  • 打赏
  • 举报
回复
说白了,就是要爬取网站的所有的 html 并且解析它。你抓取第一个 html 并且解析它,就能知道把哪些链接页面放到任务里继续爬取。
  • 打赏
  • 举报
回复
我有个图片名字叫 business.png,你来告诉我这个图片的路径吧! 这其实跟“怎样下载图片”没有多大关系。
Veary 2017-09-12
  • 打赏
  • 举报
回复
被这个问题勾起了兴趣,一般下载文件的时候,Http响应头文件中会有 Content-Disposition 说明文件的名称,但是试过几个图片都没有 Content-Disposition 这个Key,我觉得方向还是不对,持续关注。
UKnowImJin 2017-09-11
  • 打赏
  • 举报
回复
引用 2 楼 u011785544 的回复:
刚好不懂 也比较有兴趣 帮顶 你说的应该是类似用浏览器右键另存为 这时候自动生成的图片名称 对吧
对,就是那个图片地址上的源命名
UKnowImJin 2017-09-11
  • 打赏
  • 举报
回复
Veary 2017-09-11
  • 打赏
  • 举报
回复
刚好不懂 也比较有兴趣 帮顶 你说的应该是类似用浏览器右键另存为 这时候自动生成的图片名称 对吧
SK_Aqi 2017-09-11
  • 打赏
  • 举报
回复

62,244

社区成员

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

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

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

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