2

I have a folder in which there are 3-4 PDF files. SO on button click, I want to download PDF files as a ZIP file. For that I have write the below code

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;

But on button click the ZIP is not getting downloaded and the browser just loads.

What might be the cause?

10
  • can you check whether the zip file was created on the server ? Commented Jul 27, 2017 at 14:00
  • @minhhn2910: ok let me check.! Commented Jul 27, 2017 at 14:01
  • @minhhn2910: No, Now when I debugged my code, I got error as {"Could not find file 'D:\\UserName\\VSATFINAL_353\\VSATTSSRSurvey\\UploadedFiles\\I-BR-RNCH-ENB-0243_C3.zip'. at line var stream = new FileStream(strDirectory + ".zip", FileMode.Open); Commented Jul 27, 2017 at 14:04
  • @minhhn2910: hey sorry actually it created at this line ZipFile.CreateFromDirectory(strDirectory, strDirectory + ".zip"); i commented out that line. but when it already exist it gives error for me. so what should I do for this ? Commented Jul 27, 2017 at 14:10
  • 1
    Are you using MVC or web forms? If you are using MVC try FileResult or for web forms try Response.TransmitFile Commented Jul 27, 2017 at 14:19

2 Answers 2

1

You may have more luck with the Response.TransmitFile method, here is an example using the address of your zip file -

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=file.zip");
Response.TransmitFile(strFilePath + ".zip");
Response.Flush();
Sign up to request clarification or add additional context in comments.

9 Comments

let me try and check if I can get it
got this error The process cannot access the file 'D:\UserName\VSATFINAL_353\VSATTSSRSurvey\UploadedFiles\I-MH-CLSG-ENB-0001_C3.zip' because it is being used by another process. at line HttpContext.Current.Response.TransmitFile(strDirectory + ".zip");
Do you have any filestreams that aren't closed? I notice that you have a couple of lines that are now redundant - var stream = new FileStream(strDirectory + ".zip", FileMode.Open); result.Content = new StreamContent(stream);
may be, here is my full function for that. Do let me know show that I can remove the unwanted code. dotnetfiddle.net/DQChgv
Remove lines 223 and 235, these are opening filestreams that are not needed and will cause the above error
|
0

If the zip file isn't getting created you probably need to grant write/modify permissions on your UploadedFiles directory to IIS AppPool\yourAppPoolName .

1 Comment

hey, Zip file is getting created but not getting downloaded

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.