当图像下载然后得到错误“......不是一个有效的虚拟路径”

when image download then get error "... is not a valid virtual path"


在下面的代码中从 Google 中搜索图像并显示它。 我在每张下载的图片中添加了一个 link 按钮。 现在的问题是当我下载图像时出现一些错误。 错误是“...不是有效的虚拟路径。”

如何解决??

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form2" runat="server">
<div>
    <asp:TextBox ID="TextBox1" runat="server" Width="300px"></asp:TextBox>&nbsp;
    <asp:Button ID="btnSearch" runat="server" Text="Google Image Search" OnClick="Button1_Click" /><br />
    <asp:DataList ID="dlSearch" runat="server" RepeatColumns="6" CellPadding="5">
        <ItemTemplate>
            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("Url") %>'>
                <asp:Image ID="img1" src='<%#Eval("Url") %>' width="200" height="100px" runat="server" />
            </asp:HyperLink>
            <asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("Url") %>' runat="server" OnClick = "DownloadFile"></asp:LinkButton>
            <br />
        </ItemTemplate>
        <FooterTemplate>
            <asp:Label Visible='<%#bool.Parse((dlSearch.Items.Count==0).ToString())%>' runat="server"
                ID="lblNoRecord" Text="No Record Found!"></asp:Label>
        </FooterTemplate>
    </asp:DataList>       
</div>
</form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Google.API.Search;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;

public partial class ImageSearch : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        dlSearch.DataSource = null;
        dlSearch.DataBind();
        TextBox1.Text = "";
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Title", typeof(string)));
    dt.Columns.Add(new DataColumn("OriginalContextUrl", typeof(string)));
    dt.Columns.Add(new DataColumn("Url", typeof(string)));
    GimageSearchClient client = new GimageSearchClient("www.Google.com");
    IList<IImageResult> results = client.Search(TextBox1.Text, 30);
    foreach (IImageResult result in results)
    {
        DataRow dr = dt.NewRow();
        dr["Title"] = result.Title.ToString();
        dr["OriginalContextUrl"] = result.OriginalContextUrl;
        dr["Url"] = result.Url;
        dt.Rows.Add(dr);
    }        
    dlSearch.DataSource = dt;
    dlSearch.DataBind();
}

protected void DownloadFile(object sender, EventArgs e)
{
    string filePath = (sender as LinkButton).CommandArgument;
    Response.ContentType = ContentType;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
    Response.WriteFile(filePath);
    Response.End();
}
}

据我所知,有已知的方法可以获取图像等在线资源的文件名(如果图像托管提供商提供 API 那么这将给您提供其他不可能的名称)。所以你可以做的是你可以从 url 随机生成名称。

所以通常所有 URL 都在末尾包含图像名称。所以您可以使用 String methods 获取图像名称,然后将其设置为您的文件名。

  EX:- 

    string url = @"http://www.hdwallpapersinn.com/wp-content/uploads/2014/07/mumbai-india-desktop-wallpaper-660x330.jpg";
        string fileName = String.Join(string.Empty,url.Substring(url.LastIndexOf('/')+1).Split('-')); 

然后在您的回复中使用它

    Response.AppendHeader("Content-Disposition", "attachment; filename=" fileName);

更新

要从您这边提供下载,您可以根据需要从 url 获取图像数据,然后写入响应流。

WebClient client = new WebClient();
byte[] imageData = client.DownloadData(yourimageurl);

Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" fileName);
Response.ContentType = "image/JPEG";
Response.OutputStream.Write(imageData, 0, imageData.Length);
Response.End();