Aks
Aks

Reputation: 50

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


In below code image search from Google and display it. i add one link-button in every image that is download. Now problem is get some error when i download image . Error is " ... is not a valid virtual path."

how to solve it??

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

Upvotes: 0

Views: 2324

Answers (1)

Mahesh
Mahesh

Reputation: 8892

As far as I know there is know way to get file name for online resource like image (if the image hosting provider provides API then that will give you the name other wise not possible). So what you can do is you can generate the name from the url randomly.

So in general all the URL contains the image name at the end. so you can use the String methods to get the image name and then set that as the your filename.

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

and then use it in your response

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

UPDATE

To provide the download from your side you can do as you need to get the image data from the url and then write to the response stream.

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

Upvotes: 1

Related Questions