【发布时间】:2013-11-19 04:35:17
【问题描述】:
我在使用 WebClient 类下载 jpeg 图像时遇到了一些问题。我正在尝试通过以下代码下载 jpeg 图片:
public bool DownloadUri(string strUri, string destPath)
{
bool result = false;
try
{
Uri uri = new Uri(strUri, UriKind.RelativeOrAbsolute);
WebClient client = new WebClient();
client.DownloadFile(uri, destPath);
result = true;
}
catch (Exception ex)
{
result = false;
}
return result;
}
大多数工作正常,但当 jpeg 不存在时,我尝试下载的站点会重定向并且下载不会引发异常,它会下载大小仅为 8k 的损坏 jpeg。这是我提供的一个违规网址:
http://axisproperty.net.au/Upload/listing/500748870/500748870_1_500266205%2c1336368187%2cImageA.jpg
我已经使用 Uri.TryCreate 和 Uri.IsWellFormedUriString(urlAttribute.Value, UriKind.Absolute) 验证了 uri。我尝试执行 File.Exists(uri.ToString()) 来验证 uri 是否具有与其关联的有效文件,尽管即使对于有效的 url 也会失败。
有没有其他方法可以验证文件确实存在,这样我就不会下载所有这些不必要的损坏 jpeg 文件?
【问题讨论】:
标签: .net file download webclient