Reputation: 7490
I have .zip
file in file system. I want to download that file. So far I have done
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
HttpContext.Current.Response.TransmitFile(zipName);
HttpContext.Current.Response.End();
But it directly opens up the file rather than saving it. How can download it instead if saving?
I have also seen DownloadFile(String, String)
but what will be first argument in my case?
Upvotes: 0
Views: 1086
Reputation: 172588
In case you want to download it from the remote server then you can simply use the WebClient class
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteFilePath, FileName);
or
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
int bufferSize = 1;
Response.Clear();
Response.AppendHeader("Content-Disposition:", "attachment; filename=" +filename);
Response.AppendHeader("Content-Length", resp.ContentLength.ToString());
Response.ContentType = "application/download";
byte[] ByteBuffer = new byte[bufferSize + 1];
MemoryStream ms = new MemoryStream(ByteBuffer, true);
Stream rs = req.GetResponse().GetResponseStream();
byte[] bytes = new byte[bufferSize + 1];
while (rs.Read(ByteBuffer, 0, ByteBuffer.Length) > 0)
{
Response.BinaryWrite(ms.ToArray());
Response.Flush();
}
Upvotes: 0
Reputation: 191
You have to zip and than get the bytes from that zip and pass them
context.Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}.{1}", fileName, fileExtension));
context.Response.ContentType = "application/octet-stream";
context.Response.OutputStream.Write(zipBytesArray, 0, zipBytesArray.Length);
context.Response.End();
Upvotes: 1