ASP.NET使用querystring从URL下载图像文件

本文关键字:下载 图像 文件 URL NET 使用 querystring ASP | 更新日期: 2023-09-27 18:20:47

我正在尝试从URL下载图像。URL的末尾附加了一个安全密钥,我不断收到以下错误:System.Net.WebException:WebClient请求期间发生异常。--->System.ArgumentException:路径中存在非法字符

我不确定该使用什么正确的语法。下面是我的代码。

string remoteImgPath = "https://mysource.com/2012-08-01/Images/front/y/123456789.jpg?api_key=RgTYUSXe7783u45sRR";
string fileName = Path.GetFileName(remoteImgPath);
string localPath = AppDomain.CurrentDomain.BaseDirectory + "LocalFolder''Images''Originals''" + fileName;
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteImgPath, localPath);
return localPath;

ASP.NET使用querystring从URL下载图像文件

我认为这就是您想要的:

string remoteImgPath = "https://mysource.com/2012-08-01/Images/front/y/123456789.jpg?api_key=RgTYUSXe7783u45sRR";
Uri remoteImgPathUri = new Uri(remoteImgPath);
string remoteImgPathWithoutQuery = remoteImgPathUri.GetLeftPart(UriPartial.Path);
string fileName = Path.GetFileName(remoteImgPathWithoutQuery);
string localPath = AppDomain.CurrentDomain.BaseDirectory + "LocalFolder''Images''Originals''" + fileName;
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteImgPath, localPath);
return localPath;