如何压缩xml和下载 [英] how zip the xml and download
问题描述
我有一个数据集。我希望将其保存为xml并压缩以供用户下载。
我使用下面的代码。
xml文件保存但保存对话框没有出现。如何将其压缩并向用户显示保存对话框
I have a dataset .I want save it as xml and zip it for user download it .
I use code below.
the xml file saves but the save dialog doesn't appear .how can I zip it and show save dialog to user
datasetKetab dsk = new datasetKetab();
Ketab ketab = (from k in jle.Ketabs where k.KetabID == ketabid select k).Single();
dsk.Ketab.AddKetabRow(ketab.Nam, ketab.TedadeSafhe.Value, ketab.TedadeSoal.Value, ketab.Aks);
dsk.WriteXml(@"C:\Users\moslem\Desktop\a.xml");
确定它有效。 linested lonic我使用了system.io.compression.zip文件
但是我收到一个错误:该进程无法访问文件,因为zip文件名文件正由另一个进程使用
xml和zip文件在该文件夹中创建,但文件不会下载。为什么?
ok it works. inested lonic I used system.io.compression.zipfile
but I get an error:"the process cannot access file because "zip file name" the file is using by another process"
xml and zip file is created in that folder but file will not download . why?
string directoryname=MapPath("path...");
string xmlfile = MapPath("path..." + ketab.Nam + ".xml");
dsk.WriteXml(xmlfile);
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/zip";
Response.AppendHeader("content-disposition", "attachment; filename=Report.zip");
string zipfile=MapPath("path..." + ketab.Nam+".zip");
ZipFile.CreateFromDirectory(directoryname, zipfile);//error is in this line
//File.Delete(xmlfile);
Response.WriteFile(zipfile);
推荐答案
dt.TableName =声明;
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType =application / zip;
Response.AppendHeader(content-disposi
dt.TableName = "Declaration";
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/zip";
Response.AppendHeader("content-disposi
tion,attachment; filename = Report.zip);
使用(Ionic.Zip.ZipFile zipFile = new Ionic.Zip.ZipFile())
{
zipFile.AddEntry(Report.xml,(name,stream)=> dt.WriteXml(stream));
zipFile.Save(Response.OutputStream);
}
tion", "attachment; filename=Report.zip");
using(Ionic.Zip.ZipFile zipFile = new Ionic.Zip.ZipFile())
{
zipFile.AddEntry("Report.xml", (name,stream) => dt.WriteXml(stream) );
zipFile.Save(Response.OutputStream);
}
这篇关于如何压缩xml和下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!