Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未使用asp.net下载Zip文件_C#_Asp.net_Zipfile - Fatal编程技术网

C# 未使用asp.net下载Zip文件

C# 未使用asp.net下载Zip文件,c#,asp.net,zipfile,C#,Asp.net,Zipfile,我有一个文件夹,里面有3-4个pdf文件。所以,点击按钮,我想下载PDF文件作为ZIP文件。为此,我编写了以下代码 string strFilePath = HttpContext.Current.Server.MapPath("~/UploadedFiles/" + SAP_ID + '_' + CANDIDATEID + "\\" + SAP_ID + '_' + CANDIDATEID + ".pdf"); string strDirectory = HttpContext.Current

我有一个文件夹,里面有
3-4个pdf
文件。所以,点击按钮,我想下载PDF文件作为
ZIP
文件。为此,我编写了以下代码

string strFilePath = HttpContext.Current.Server.MapPath("~/UploadedFiles/" + SAP_ID + '_' + CANDIDATEID + "\\" + SAP_ID + '_' + CANDIDATEID + ".pdf");
string strDirectory = HttpContext.Current.Server.MapPath("~/UploadedFiles/" + SAP_ID + '_' + CANDIDATEID);

HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

if (Directory.Exists(strDirectory))
{
    if (File.Exists(strFilePath + ".zip"))
    {
        var filestream = new System.IO.FileStream(strFilePath + ".zip", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
        filestream.Close();
        File.Delete(strFilePath + ".zip");
     }                   
}

//  ZipFile.CreateFromDirectory(strDirectory, strDirectory + ".zip");

var stream = new FileStream(strDirectory + ".zip", FileMode.Open);

result.Content = new StreamContent(stream);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(strDirectory + ".zip");
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;
但点击按钮后,ZIP不会被下载,浏览器只是加载


请帮助我了解原因?

如果没有创建zip文件,您可能需要将UploadedFiles目录上的写入/修改权限授予IIS AppPool\yourAppPoolName

使用Response.TransmitFile方法可能会更幸运,下面是一个使用zip文件地址的示例-

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=file.zip");
Response.TransmitFile(strFilePath + ".zip");
Response.Flush();

你能检查一下zip文件是否是在服务器上创建的吗?@minhhn2910:好的,让我检查一下@minhhn2910:不,现在当我调试我的代码时,我得到的错误是
{“找不到文件'D:\\UserName\\VSATFINAL\u 353\\vsattssrreasy\\UploadedFiles\\I-BR-RNCH-ENB-0243\u C3.zip.
第行
var stream=new FileStream(strDirectory+“.zip”,FileMode.Open);
@minhhn2910:hey抱歉,实际上它是在这一行创建的
ZipFile.CreateFromDirectory(strDirectory,strDirectory+“.zip”)
我注释掉了那一行。但是当它已经存在时,它会给我带来错误。那么我应该怎么做呢?你是使用MVC还是web表单?如果你使用MVC,请尝试使用FileResult还是web表单,请尝试响应。TransmitFile嘿,Zip文件正在创建,但没有下载让我试试看是否可以得到它。获取此错误
程序ess无法访问文件“D:\UserName\VSATFINAL\u 353\vsattssrreasy\UploadedFiles\I-MH-CLSG-ENB-0001\u C3.zip”,因为另一个进程正在使用它。
行的HttpContext.Current.Response.TransmitFile(strDirectory+“.zip”)
您是否有未关闭的文件流?我注意到您有两行现在是多余的-var stream=new FileStream(strDirectory+“.zip”,FileMode.Open);result.Content=new StreamContent(stream);可能是,这是我的全部功能。一定要让我知道我可以删除不需要的代码。删除第223行和第235行,这些是打开不需要的文件流,将导致上述错误