0

有没有办法下载用户上传的图像?我有以下内容:首先,用户单击马赛克中的图像以详细查看它;然后图像在 asp:image 标记中生成,在该步骤中,他们应该通过单击按钮来下载它。我遇到的问题是图像是在 asp:image 标签中生成的,而不是带有名称的实际图像本身。我必须下载的代码只获取存储在文件夹中的图像。下面是代码:

protected void btnDownload_Click(object sender, EventArgs e)
{
    string path = @"C:\inetpub\wwwroot\PSCSearchEngine\MemberPages\Images\live.jpg";        
    System.IO.FileInfo file = new System.IO.FileInfo(path);
    if (file.Exists)
    {
        Response.Clear();
        Response.ContentType = MimeType(Path.GetExtension(path));
        Response.AddHeader("Content-Disposition", 
            string.Format("attachment; filename = {0}",
            System.IO.Path.GetFileName(path)));
        Response.AddHeader("Content-Length", file.Length.ToString("F0"));
        Response.TransmitFile(path);
        Response.End();
    }
    else
    {
        Response.Write("This file does not exist.");
    } 
}

下面是预览图像的代码:

@"~/MemberPages/UpdatePhoto.aspx?SiteKey=" + foo.Site_ID 
    + "&TimeStamp=" + foo.timestamp[n1 - 1];
4

2 回答 2

1

在 C# 中单击按钮下载图像文件的代码

protected void btnDownload_Click(object sender, EventArgs e)
{
    string filename=MapPath("birds.jpg");
    Response.ContentType = "image/JPEG";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + filename+ "");

    Response.TransmitFile(filename);
    Response.End();
}
于 2013-09-07T11:02:56.657 回答
0

你可以试试这个:

using (var client = new System.Net.WebClient())
{
    var _imagebytes = client.DownloadData(string.Format(@"~/MemberPages/UpdatePhoto.aspx?SiteKey={0}&TimeStamp={1}", foo.Site_ID, foo.timestamp[n1 - 1]));
    Response.Clear();
    Response.ContentType = "image/jpg";
    Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", "MyImage.jpg"));
    Response.AddHeader("Content-Length", _imagebytes.Length.ToString("F0"));
    Response.OutputStream.Write(_imagebytes, 0, _imagebytes.Length);
    Response.End();
}
于 2017-09-02T09:59:35.893 回答