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

时间:2012-05-31 10:25:22

标签: c# asp.net

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

2 个答案:

答案 0 :(得分:0)

您可以使用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")'

答案 1 :(得分:0)

您应该使用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 )具有访问权限写入该目录。

相关问题