简体   繁体   中英

Download file from virtual path URL asp.net

I have a virtual path URL (http://xyz.com/eRoom/SPOmidrangesysdiv/EFT3%20Test%20Scorecard%20for%20Inyo.xlsx). Credentials required to brow this url. I want to download file from url using ASP.NET C#.

You can use 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")'

You should use WebClient as Asif mentioned above. Here's it broken down to easily substitute alternate files in same base location.

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);        

You can specify a directory in the second parameter of the DownloadFile method, just make sure your IIS user (typcially SERVERNAME \\IUSR_ SERVERNAME ) has access to write to that directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM