【问题标题】:Download File C#下载文件 C#
【发布时间】: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


【解决方案1】:

我认为您想将图像下载到客户端的计算机,而不是服务器

您拥有的代码会将文件下载到服务器。不是你想要的。要将内容发送到客户端,请使用 Response 对象。

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

它的作用是获取图像的路径(我假设你知道怎么做),然后告诉它content type 是什么(我假设是 JPEG,但你可能有 PNG 或 GIF 等),然后添加一个标头,告诉它正在发送一个建议文件名为myimage.jpeg 的文件。然后我们将文件写入客户端,并结束响应。

【讨论】:

    【解决方案2】:
    1. 确保您在 (web.config) &lt;trust level="Full"/&gt; 上具有完全信任级别
    2. 在您的 DownloadFile 的第二个参数中添加以下代码:
        protected void btnDownload_Click(object sender, ImageClickEventArgs e)
        {
              .....
              theWebClient.DownloadFile("IMG_0417.JPG", 
                                        HttpRuntime.AppDomainAppPath + "\\IMG_0417.JPG");
              .....
        }
    

    好的,以上答案完全基于用户的代码问题。以下解决方案将是 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;
        }
    

    【讨论】:

    • 我投了反对票,因为 OP 的问题强烈表明他们希望将文件下载到客户端计算机,而不是下载到服务器。您的代码可能会让他们更加困惑。
    • 感谢@mason 让我知道,我基本上是在检查他的代码,而不是为 OP 提供解决他问题的方法。我已经更新了我的答案,希望它不会让他感到困惑。
    • 我确实希望将文件下载到用户计算机上。不是服务器。
    猜你喜欢
    • 1970-01-01
    • 2015-03-23
    • 2011-09-24
    • 2016-12-29
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多