您的位置:首页 > 编程语言 > ASP

asp.net 把图片压缩成zip之后再进行下载

2016-01-13 15:13 435 查看
//这是导出的js方法

function fundaochu() {
var data = "keyword=GetImageListdaochu&type=daochu&modality=" + $(eventRow).attr("study_date") + "&strWhere=a.REGISTRATION_ID=" + $(eventRow).attr("reg_id") + " and a.modality='" + $(eventRow).attr("modality") + "' order by INSTANCE_NUMBER DESC";
var renamefunc = function (msg) {

if (msg != null && msg != "") {
//我是通过一个固定地址,然后把形成的压缩包的名字传到前台来,然后通过固定地址加上zip的名字然后下载
var name = unescape(msg);
//这是当最后一位数传过来的是2的时候,就证明没有文件需要下载,弹出该人物没有图像可供下载
if (name.substring(name.length - 1, name.length) == "2") {
alert(name.substring(0, name.length - 1));
}
else {
//传过来的数据必须去掉最后一位,因为最后一位我是拼接了个1或者2,1是有数据可以下载,2是无数据可以下载
var newname = name.substring(0, name.length - 1);
//通过一个新的页面把这个zip文件打开下载
var fileURL = window.open("../linshi/" + newname + ".zip", "_blank", "height=0,width=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no");
//var fileURL = window.open("../lishi", "_blank", "height=0,width=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no");
//紧接着关闭这个小页面
fileURL.window.close();
fileURL.close();
//这是最终下载的代码
fileURL.document.execCommand("SaveAs");

}

}

};
CommonAjax(renamefunc, null, data);
}


  

//这是图片下载转换的方法
public string getImgdaochu(HttpRequest request)
{
PATS_DAL dall = new PATS_DAL();
try
{
string url = string.Empty;
string zipname = string.Empty;
string tupian_url = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
string where = request.Params["strWhere"].ToString();
string type = request.Params["type"].ToString();
HttpContext context = HttpContext.Current;
//每次下载的时候都会把文件放到这个文件夹下然后前台通过这个地址进行下载,所以我每次都会删除上次导出时候创建的文件夹,然后在重新创建,这样做的效果就是这个文件夹里永远只有一个最新的文件;
//这里放的是jpeg格式的图片,因为我从服务器上获取的不是jpeg的文件,是dicm的文件,我需要把这个dicm文件转换成jpeg转换成之后,我需要临时存放到这个文件夹里面以备后面压缩。
string weburl = "d:\\webtupian";
if (Directory.Exists(weburl))
{
DirectoryInfo direct = new DirectoryInfo(weburl);
direct.Delete(true);
}
//这个是压缩包zip存放的地方
string lurl = HttpContext.Current.Server.MapPath("../linshi");
if (Directory.Exists(lurl))
{
DirectoryInfo direct1 = new DirectoryInfo(lurl);
direct1.Delete(true);
}
string modality = "";
if (request.Params["modality"] != null && request.Params["modality"].ToString() != "")
{
modality = request.Params["modality"].ToString();
}
//获取特定的数据集信息
DataSet ds = dall.getimgdaochu(where, modality);
StringBuilder html = new StringBuilder();

string zipedFile = string.Empty;
List<Byte[]> lstFI = new List<Byte[]>();
List<string> listimage = new List<string>();
byte[] img = null;
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
int i = 1;
foreach (DataRow row in ds.Tables[0].Rows)
{
context.Response.ClearContent();
string dcm_path = row["REFERENCE_FILE"].ToString();//这是dicm文件的地址
string share_ip = SYS_APP_CONFIG.GetAppConfigValue("dcm_share_ip");//这是服务器的ip地址
zipedFile = row["PATIENT_ID"].ToString();//这是数据的编号。
string tuname = row["PATIENT_NAME"].ToString();//这是当前这个数据人的名字
img = DicomUtility.GetJpegFromDcm(dcm_path, System.Drawing.Imaging.ImageFormat.Jpeg);//这是通过dicm文件的地址和ip什么的,把dicm文件转换成图片格式的byte字节。
Image imgg = BytToImg(img);//这是通过byte字节获取jpeg的图片
zipname = tupian_url + tuname + i.ToString() + "张";//压缩zip的名字
url = "d:\\webtupian";
if (!Directory.Exists(url))
{
Directory.CreateDirectory("d:\\webtupian");//创建新路径
}
string name = tuname + i.ToString();
imgg.Save("d:\\webtupian" + "/" + name + ".jpeg");//图片保存
listimage.Add("d:\\webtupian" + "/" + name + ".jpeg");//把当前图片保存的路径保存起来,用于后面的zip通过路径压缩文件。
i++;
imgg.Dispose();
}
//压缩成zip,需要下载ionic的dll然后引用。
//zipname这个是zip的名字
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(zipname, Encoding.Default))
{
foreach (string fileToZip in listimage)
{
using (FileStream fs = new FileStream(fileToZip, FileMode.Open, FileAccess.ReadWrite))//根据路径把文件转换成文件流
{
byte[] buffer = new byte[fs.Length];//把文件流转换成byte字节
fs.Read(buffer, 0, buffer.Length);
string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
zip.AddEntry(fileName, buffer);
}
}
if (!Directory.Exists(HttpContext.Current.Server.MapPath("../linshi")))
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath("../linshi"));//创建新路径
}
zip.Save(HttpContext.Current.Server.MapPath("../linshi/" + zipname + ".zip"));
}
//为了防止乱码,把数据封装了一下,到前台用unescape()解析就可以了。
return Microsoft.JScript.GlobalObject.escape(zipname + "1");//返回zip的名字和拼接的字符串1,1是代表有数据可以导出
}
else
{
return "当前该人员没有图片可供导出。2";
}
}
catch (Exception e)
{
Utility.LOG.ERRORLOG.Add(2, e, "", RisConfig.pacsAppVer);
return "非常抱歉,系统出现错误!具体请查看PACS错误日志列表!2";
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: