gpt4 book ai didi

c# - 如何使用asp.net从远程服务器下载文件

转载 作者:行者123 更新时间:2023-11-30 17:22:58 26 4
gpt4 key购买 nike

下面的代码可以很好地从当前电脑下载文件。请告诉我如何使用 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);

关于c# - 如何使用asp.net从远程服务器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2460137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com