从虚拟路径 URL asp.net 下载文件

本文关键字:net 下载 文件 asp URL 虚拟 路径 | 更新日期: 2023-09-27 18:35:56

我有一个虚拟路径URL(http://xyz.com/eRoom/SPOmidrangesysdiv/EFT3%20Test%20Scorecard%20for%20Inyo.xlsx)。浏览此 URL 所需的凭据。我想使用 ASP.NET C# 从 url 下载文件。

从虚拟路径 URL asp.net 下载文件

您可以使用 WebClient

WebClient client = new System.Net.WebClient();
// You might require some headers to be added for authentication
client.AddHeader("header", "header"); 
byte[] data = client.DownloadData("http://xyz.com/eRoom/SPOmidrangesysdiv/EFT3%20Test%20Scorecard%20for%20Inyo.xlsx")'

您应该使用 WebClient 作为上面提到的 Asif。 以下是细分为轻松替换同一基本位置中的备用文件。

string remoteUri = "http://xyz.com/eRoom/SPOmidrangesysdiv/";
string fileName = "EFT3%20Test%20Scorecard%20for%20Inyo.xlsx", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
myWebClient.DownloadFile(myStringWebResource,fileName);        

您可以在 DownloadFile 方法的第二个参数中指定一个目录,只需确保您的 IIS 用户(通常是 SERVERNAME''IUSR_SERVERNAME)有权写入该目录即可。