[英]Download file Asp.Net
我在文件系統中有.zip
文件。 我要下載該文件。 到目前為止,我已經完成了
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
HttpContext.Current.Response.TransmitFile(zipName);
HttpContext.Current.Response.End();
但是它直接打開文件而不是保存文件。 保存后如何下載呢?
我也看過DownloadFile(String, String)
但是在我的情況下,第一個參數是什么?
您必須壓縮,然后從該壓縮中獲取字節並將其傳遞
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();
如果您想從遠程服務器下載它,則只需使用WebClient類
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteFilePath, FileName);
要么
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.