【发布时间】:2014-05-26 02:15:14
【问题描述】:
所以我不敢相信我会问这个问题。这似乎应该非常直截了当。我在一个网站上有一个相册,它基本上向用户显示了 3 个不同的按钮:上一张图片、下一张图片和下载当前图片。
除“下载图像”按钮外,一切正常。我所有的研究都让我回到使用WebClient 方法,这似乎非常简单。但是,当我设置它时,我不断收到
System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Net.WebClient.DownloadFile(Uri address, String fileName) at System.Net.WebClient.DownloadFile(String address, String fileName) at mySite.Pictures.USMC.MarineCorpsGeneral.btnDownload_Click(Object sender, ImageClickEventArgs e) in C:\Users\myFile.aspx.cs:line 124 The action that failed was: Demand The type of the first permission that failed was: System.Security.Permissions.FileIOPermission The Zone of the assembly that failed was: MyComputer
这个错误看起来是某种权限问题,但我已经为几乎所有人提供了上帝般的访问权限,但仍然没有成功。我已经准备好了关于在 web 配置中将信任级别添加到 Full 的线程,但这也不起作用。这是我的代码:
protected void btnDownload_Click(object sender, ImageClickEventArgs e)
{
Response.Write("File Path: " + lblCenterImage.Text + "<br>");
Response.Write("File Name: " + lblCenterImage.Text.Substring(lblCenterImage.Text.LastIndexOf("/") + 1, lblCenterImage.Text.Length - lblCenterImage.Text.LastIndexOf("/") - 1));
using (WebClient theWebClient = new WebClient())
{
try
{
theWebClient.Proxy = null;
theWebClient.BaseAddress = "http://website/files/images/";
theWebClient.Credentials = new NetworkCredential("user", "pass");
theWebClient.DownloadFile("IMG_0417.JPG", "IMG_0417.JPG");
}
catch (Exception excep)
{
Response.Write(excep.ToString());
}
}
}
所以我的问题是这样的:
从 URL 下载图像最简单的方法是什么?如果这可以在 JavaScript 中完成,那也可以。
【问题讨论】:
-
你不能那样做。您需要了解客户端代码和服务器端代码之间的区别。
标签: c# javascript asp.net webclient