Asp.net mvc 3 将FileContentResult文件导出到ZIP
我使用C#-MVC3。我有一个“导出”页面。我有一些从数据库导出不同表的函数,每个函数都从表中创建一个CSV文件,并向用户返回一个FileContentResult文件 现在我想创建一个“全部导出”按钮,一次下载所有文件。 我尝试使用ZipFile,但它只获取保存在服务器上的文件名和路径,而不是“FileContentResult”文件 所以我想在服务器上临时保存“FileContentResult”文件,压缩并删除它们——但我找不到如何保存“FileContentResult”文件 如果您能帮助我或给我另一个想法,我很高兴听到。我的解决方案:Asp.net mvc 3 将FileContentResult文件导出到ZIP,asp.net-mvc-3,zipfile,Asp.net Mvc 3,Zipfile,我使用C#-MVC3。我有一个“导出”页面。我有一些从数据库导出不同表的函数,每个函数都从表中创建一个CSV文件,并向用户返回一个FileContentResult文件 现在我想创建一个“全部导出”按钮,一次下载所有文件。 我尝试使用ZipFile,但它只获取保存在服务器上的文件名和路径,而不是“FileContentResult”文件 所以我想在服务器上临时保存“FileContentResult”文件,压缩并删除它们——但我找不到如何保存“FileContentResult”文件 如果您能帮
public ZipFile DownloadAllToZip()
{
string path = "c:\\TempCSV";
try
{
if (Directory.Exists(path))
{
EmptyFolder(path);
}
else
{
DirectoryInfo di = Directory.CreateDirectory(path);
}
List<FileContentResult> filesToExport = GetAllCSVs();
foreach (var file in filesToExport)
{
try
{
using (FileStream stream = new FileStream(path + "\\" + file.FileDownloadName, FileMode.CreateNew))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{
byte[] buffer = file.FileContents;
stream.Write(buffer, 0, buffer.Length);
writer.Close();
}
}
}
catch { }
}
}
catch{ }
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=MaterialAssetTracker.zip");
ZipFile zip= new ZipFile();
using (zip)
{
zip.CompressionLevel = CompressionLevel.None;
zip.AddSelectedFiles("*.csv", path + "\\", "", false);
zip.Save(Response.OutputStream);
}
Response.Close();
EmptyFolder(path);
return zip;
}
public ZipFile DownloadAllToZip()
{
string path=“c:\\tempcv”;
尝试
{
if(目录存在(路径))
{
清空文件夹(路径);
}
其他的
{
DirectoryInfo di=Directory.CreateDirectory(路径);
}
列出filesToExport=GetAllCSVs();
foreach(filesToExport中的var文件)
{
尝试
{
使用(FileStream stream=newfilestream(路径+“\\”+file.FileDownloadName,FileMode.CreateNew))
{
使用(StreamWriter=newstreamwriter(stream,Encoding.UTF8))
{
字节[]缓冲区=file.FileContents;
stream.Write(buffer,0,buffer.Length);
writer.Close();
}
}
}
捕获{}
}
}
捕获{}
Response.Clear();
Response.BufferOutput=false;
Response.ContentType=“应用程序/zip”;
AddHeader(“内容处置”、“附件;文件名=materialassetracker.zip”);
ZipFile zip=新ZipFile();
使用(zip)
{
zip.CompressionLevel=压缩级别。无;
zip.AddSelectedFiles(“*.csv”,路径+“\\”,“”,false);
Save(Response.OutputStream);
}
Response.Close();
清空文件夹(路径);
返回拉链;
}
我的解决方案:
public ZipFile DownloadAllToZip()
{
string path = "c:\\TempCSV";
try
{
if (Directory.Exists(path))
{
EmptyFolder(path);
}
else
{
DirectoryInfo di = Directory.CreateDirectory(path);
}
List<FileContentResult> filesToExport = GetAllCSVs();
foreach (var file in filesToExport)
{
try
{
using (FileStream stream = new FileStream(path + "\\" + file.FileDownloadName, FileMode.CreateNew))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{
byte[] buffer = file.FileContents;
stream.Write(buffer, 0, buffer.Length);
writer.Close();
}
}
}
catch { }
}
}
catch{ }
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=MaterialAssetTracker.zip");
ZipFile zip= new ZipFile();
using (zip)
{
zip.CompressionLevel = CompressionLevel.None;
zip.AddSelectedFiles("*.csv", path + "\\", "", false);
zip.Save(Response.OutputStream);
}
Response.Close();
EmptyFolder(path);
return zip;
}
public ZipFile DownloadAllToZip()
{
string path=“c:\\tempcv”;
尝试
{
if(目录存在(路径))
{
清空文件夹(路径);
}
其他的
{
DirectoryInfo di=Directory.CreateDirectory(路径);
}
列出filesToExport=GetAllCSVs();
foreach(filesToExport中的var文件)
{
尝试
{
使用(FileStream stream=newfilestream(路径+“\\”+file.FileDownloadName,FileMode.CreateNew))
{
使用(StreamWriter=newstreamwriter(stream,Encoding.UTF8))
{
字节[]缓冲区=file.FileContents;
stream.Write(buffer,0,buffer.Length);
writer.Close();
}
}
}
捕获{}
}
}
捕获{}
Response.Clear();
Response.BufferOutput=false;
Response.ContentType=“应用程序/zip”;
AddHeader(“内容处置”、“附件;文件名=materialassetracker.zip”);
ZipFile zip=新ZipFile();
使用(zip)
{
zip.CompressionLevel=压缩级别。无;
zip.AddSelectedFiles(“*.csv”,路径+“\\”,“”,false);
Save(Response.OutputStream);
}
Response.Close();
清空文件夹(路径);
返回拉链;
}