下载文件C#
所以我不敢相信我会问这个问题。这似乎应该是非常直截了当的。我在一个网站上有一个相册,它基本上向用户显示了3个不同的按钮:上一张图片、下一张图片和下载当前图片 除了“下载图像”按钮外,一切正常。我所有的研究都指向我使用下载文件C#,c#,javascript,asp.net,webclient,C#,Javascript,Asp.net,Webclient,所以我不敢相信我会问这个问题。这似乎应该是非常直截了当的。我在一个网站上有一个相册,它基本上向用户显示了3个不同的按钮:上一张图片、下一张图片和下载当前图片 除了“下载图像”按钮外,一切正常。我所有的研究都指向我使用WebClient方法,这似乎非常简单。然而,当我设置这个时,我不断地得到 System.Security.SecurityException:请求“System.Security.Permissions.FileIOPermission,mscorlib,Version=4.0.0
WebClient
方法,这似乎非常简单。然而,当我设置这个时,我不断地得到
System.Security.SecurityException:请求“System.Security.Permissions.FileIOPermission,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a561934e089”类型的权限失败。在mySite.Pictures.USMC.MarineCorpsGeneral.btnDownload\u单击(对象发送方,ImageClickEventArgs e)的System.Net.WebClient.DownloadFile(Uri地址,字符串文件名)在System.Net.WebClient.DownloadFile(字符串地址,字符串文件名)在mySite.Pictures.USMC.MarineCorpsGeneral.btnDownload在C:\Users\myFile.aspx.cs:line 124中,失败的操作是:请求失败的第一个权限的类型是:System.Security.Permissions.FileIOPermission失败的程序集区域是: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());
}
}
}
protectedvoidbtndownload\u单击(对象发送者,ImageClickEventArgs e)
{
Write(“文件路径:+lblCenterImage.Text+”
”;
Response.Write(“文件名:”+lblCenterImage.Text.Substring(lblCenterImage.Text.LastIndexOf(“/”)+1,lblCenterImage.Text.Length-lblCenterImage.Text.LastIndexOf(“/”-1));
使用(WebClient theWebClient=new WebClient())
{
尝试
{
theWebClient.Proxy=null;
WebClient.BaseAddress=”http://website/files/images/";
theWebClient.Credentials=新网络凭据(“用户”、“通过”);
下载文件(“IMG_0417.JPG”、“IMG_0417.JPG”);
}
捕获(例外例外)
{
Response.Write(ToString()除外);
}
}
}
所以我的问题是:
从URL下载图像最简单的方法是什么?如果这是可以在JavaScript中完成的,那么也可以
好,以上答案完全基于用户的代码问题。以下解决方案将是OP的答案。请考虑以下解决方案<强>
我想您是想将图像下载到客户端计算机,而不是服务器 您拥有的代码将文件下载到服务器。不是你想要的。要将内容发送到客户端,请使用响应对象
protected void btnDownload_Click(object sender, ImageClickEventArgs e)
{
string imagepath= GetImagePath();//blah blah, get this from somewhere
Response.ContentType= "image/JPEG";
Response.AddHeader("Content-Disposition", "attachment; filename=myimage.jpeg");
Response.WriteFile(imagepath);
Response.End();
}
这样做的目的是获取图像的路径(我想你知道怎么做),然后告诉它是什么(我假设是JPEG,但你可能有PNG或GIF等),然后添加一个头,告诉它正在发送一个建议文件名为
myimage.JPEG
的文件。然后我们将文件写入客户端,并结束响应。您不能这样做。你需要理解客户端代码和服务器端代码之间的区别。我投了反对票,因为OP的问题强烈建议他们要将文件下载到客户端计算机,而不是下载到服务器。你的代码可能会让他们更加困惑。谢谢你让我知道@mason,我基本上是在检查他的代码,而不是给OP一个解决问题的方法。我已经更新了我的答案,我希望这不会让他感到困惑。我确实想把文件下载到用户的电脑上。不是服务器。
protected void btnDownload_Click(object sender, EventArgs e)
{
try
{
linkBtnDownload.Enabled = false;
Response.Clear();
Response.ContentType = FileContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + >FileName);
//Writes the specified file directly to an HTTP response output stream, without buffering it in memory
Response.TransmitFile(MapPathReverse(URL)); //you can pass your full server path here
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();
}
catch (Exception ex)
{
//display error ...
}
}
public static string MapPathReverse(string fullServerPath)
{
string vPath = @"~\" +
fullServerPath.Replace(HttpContext.Current.Request.PhysicalApplicationPath, >String.Empty);
return vPath;
}
protected void btnDownload_Click(object sender, ImageClickEventArgs e)
{
string imagepath= GetImagePath();//blah blah, get this from somewhere
Response.ContentType= "image/JPEG";
Response.AddHeader("Content-Disposition", "attachment; filename=myimage.jpeg");
Response.WriteFile(imagepath);
Response.End();
}