下载文件c#

本文关键字:文件 下载 | 更新日期: 2023-09-27 18:04:58

我真不敢相信我会问这个问题。这看起来应该非常直接。我在一个网站上有一个相册,基本上向用户展示了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下载图像最简单的方法是什么?

下载文件c#

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

您拥有的代码将下载文件到服务器。不是你想要的。要向客户端发送内容,可以使用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();
}

这所做的是获得路径到图像(我假设你知道如何做到这一点),然后告诉它的内容类型是什么(我假设JPEG,但你可能有PNG或GIF等),然后添加了一个头,告诉它正在发送一个文件与建议的文件名myimage.jpeg。然后我们将文件写入客户端,并结束响应。

  1. 确保您在(web.config) <trust level="Full"/>上具有完全信任级别
  2. 在下载文件的第二个参数中添加下面的代码:
    protected void btnDownload_Click(object sender, ImageClickEventArgs e)
    {
          .....
          theWebClient.DownloadFile("IMG_0417.JPG", 
                                    HttpRuntime.AppDomainAppPath + "''IMG_0417.JPG");
          .....
    }

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