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

时间:2015-02-16 06:12:00

标签: c# asp.net download


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

如何解决?

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

1 个答案:

答案 0 :(得分:1)

据我所知,有一种方法可以获取像图像这样的在线资源的文件名(如果图像托管服务提供商提供了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);

<强>更新

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

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