C#下载图片,用户选择保存路径
2024-08-31 11:12:51
Html代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<a href="ImgDownloadHandler.ashx">下载</a>
<img src="http://10.1.18.59/CenterPointService/RainfallCenterTemp/20180319155952/20170601__20170930.png"
alt="RainfallCenter" id="RainfallCenter" />
</body>
</html>
C#一般处理程序代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.MobileControls;
using System.Drawing;
using System.IO;
using System.Net; namespace WebApplication1
{
/// <summary>
/// ImgDownloadHandler 的摘要说明
/// </summary>
public class ImgDownloadHandler : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
#region action 2 可以导出图片
Stream stream = null;
string UrlImg = "http://10.1.18.59/CenterPointService/RainfallCenterTemp/20180319155952/20170601__20170930.png";
WebClient webClient = new WebClient();
webClient.Credentials = CredentialCache.DefaultCredentials;
//以数组的形式下载指定文件
byte[] byteData = webClient.DownloadData(UrlImg);
stream = BytesToStream(byteData);
string fileName = "20170601__20170930.png";//客户端保存的文件名
context.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
context.Response.BinaryWrite(byteData);
context.Response.Flush();
context.Response.End();
#endregion
}
/// <summary>
/// 将二进制转化为数据流
/// </summary>
/// <param name="bytes">二进制数组</param>
/// <returns></returns>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
/// <summary>
/// 将流转化为二进制数组
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, , bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(, SeekOrigin.Begin);
return bytes;
} public bool IsReusable
{
get
{
return false;
}
}
}
}
最新文章
- ENode 2.6 架构与设计简介以及全新案例分享
- golang和vim-go安装配置
- 【Beta版本】冲刺-Day6
- 【HDU】4405 Aeroplane chess
- GIT(分布式版本控制系统)
- IOS开发-项目实战-点赞功能的实现
- udp内网穿透 两个内网互联
- android 源码目录介绍
- hdu1059 Dividing ——多重背包
- Redis实战
- C语言基础知识-循环结构
- WEB开发时Browser控件得到C:\fakepath\ 的解决方式
- denoising autoencoder
- CentOS上安装WordPress搭建博客平台
- Linux后门账户控制
- B树的查找、插入、删除(附源代码)
- 【论文:麦克风阵列增强】Speech Enhancement Based on the General Transfer Function GSC and Postfiltering
- java多线程知识点概述
- Linux常用命令(二)--文件目录命令
- SQL随记(三)