2017-07-27 83 views
2

我有一个文件夹中有3-4 pdf文件。 SO按钮点击,我想下载PDF文件作为ZIP文件。为此,我写下面的代码Zip文件没有越来越下载使用asp.net

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没有得到下载和浏览器刚加载。

请帮我知道是什么原因?

+0

可以检查zip文件是否在服务器上创建的? – minhhn2910

+0

@ minhhn2910:好的,让我检查。 – BNN

+0

@ minhhn2910:没有,现在,当我调试我的代码,我得到了错误的'{“找不到文件“d:\\ \\用户名\\ VSATFINAL_353 \\ VSATTSSRSurvey \\ UPLOADEDFILES I-BR-RNCH-ENB-0243_C3 .zip'.'在行'变种流=新的FileStream(strDirectory + “的.zip”,FileMode.Open);' – BNN

回答

1

您可能有更多的运气与Response.TransmitFile方法,这里是使用zip文件的地址的例子 -

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

让我尝试和检查,如果我能得到它 – BNN

+0

得到这个错误'的进程无法访问该文件“d:\用户名\ VSATFINAL_353 \ VSATTSSRSurvey \ UPLOADEDFILES \ I-MH- CLSG-ENB-0001_C3.zip”,因为它是在线路正由另一个process.''HttpContext.Current.Response.TransmitFile(strDirectory +‘.ZIP’);' – BNN

+0

你有未关闭的任何文件流?我注意到你有几行现在是多余的 - var stream = new FileStream(strDirectory +“.zip”,FileMode.Open); result.Content = new StreamContent(stream); –

0

如果zip文件没有得到创建你可能需要授予写入/在你UPLOADEDFILES目录IIS应用程序池\ yourAppPoolName修改权限。

+0

哎,是越来越创建Zip文件,但没有得到下载 – BNN