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?
{"Could not find file 'D:\\UserName\\VSATFINAL_353\\VSATTSSRSurvey\\UploadedFiles\\I-BR-RNCH-ENB-0243_C3.zip'.at linevar stream = new FileStream(strDirectory + ".zip", FileMode.Open);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 ?