[英]how to download a file from remote server using asp.net
以下代碼適用於從當前pc.plz下載文件建議我如何使用IP地址或任何方法從遠程服務器下載它
protected void Button1_Click(object sender, EventArgs e)
{
const string fName = @"C:\ITFSPDFbills\February\AA.pdf";
FileInfo fi = new FileInfo(fName);
long sz = fi.Length;
Response.ClearContent();
Response.ContentType = MimeType(Path.GetExtension(fName));
Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(fName)));
Response.AddHeader("Content-Length", sz.ToString("F0"));
Response.TransmitFile(fName);
Response.End();
}
public static string MimeType(string Extension)
{
string mime = "application/octetstream";
if (string.IsNullOrEmpty(Extension))
return mime;
string ext = Extension.ToLower();
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (rk != null && rk.GetValue("Content Type") != null)
mime = rk.GetValue("Content Type").ToString();
return mime;
}
這樣做會更容易:
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteFileUrl, localFileName);
您可以使用HttpWebRequest
,如下所示:
Uri uri = new Uri(""); // Here goes uri to the file.
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
{
// Process response.
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.