We help IT Professionals succeed at work.

Open/Save Dialog box

2,037 Views
Last Modified: 2008-02-01
I have a link in my ASP.Net application for downloading some content. When the user clicks on this link he should be presented with an Open/Save dialog box. If user clicks on Save he should be able to save the file to his local file system (The dialog box may be the default one which windows/IE provides).

The URL of the file which needs to be saved is being fetched in a variable (in aspx page)

Say,
content_URL="http://www.abc.com/sample/sample1.mp3


In my code I have
<a href='" + content_URL + "'><img src='../images/download.JPG' border=0></a>";

When the user clicks on this link he should be able to save the file. Please tell me how to do it. Right now if he clicks on the link it opens in his default media player and he does not get an option to save it.
Thank You
Comment
Watch Question

Commented:
see this thread:
https://www.experts-exchange.com/Web_Development/Miscellaneous/Q_21764128.html?qid=21764128

it schould help if you generate files yourself
if not - files are somewhere in your filesystem, you schould try to adjust your webserver configuration to set proper headers.

Author

Commented:
Files are coming from some URL (external site). As mentioned above, the value that we get in the variable (say, content_URL) is something like "http://www.abc.com/sample/sample1.mp3".

So the anchor tag gets resolved to

<a href='http://www.abc.com/sample/sample1.mp3'><img src='../images/download.JPG' border=0></a>";

Commented:
Try this
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="aspx_Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <%  
    string filePath = Server.MapPath("D:\\ASPtoASPX.doc");
    System.IO.FileInfo targetFile = new System.IO.FileInfo(filePath);

    if (targetFile.Exists)
    {
        Response.Clear;
        Response.AddHeader("Content-Disposition", "attachment; filename=" + targetFile.Name);
        Response.AddHeader("Content-Length", targetFile.Length.ToString);
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(targetFile.FullName);

    }
%>
</body>
</html>

Author

Commented:
Does not work
You can use an intermediate page, call it download.aspx
In that page use the Content-Disposition Header to make the resource download
following is an example of 2 pages the first (default.aspx) has a link, the second (download.aspx) is where the link points to.
//Default.aspx
<body>
    <form id="form1" runat="server">
    <div>
    <a href="download.aspx?resource=Allegretto.mp3">click</a>
    </div>
    </form>
</body>
 
//Download.aspx.cs
protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Response.AddHeader("Content-Disposition", "attachment; filename="+Request.QueryString["resource"]);
    }

Open in new window

Author

Commented:
I wrote this in my aspx page
   <form id="Form1" runat="server">
<asp:Button ID="Button1" OnClick="btnDownload_Click" Text="Submit" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>


and this in corresponding cs file
protected void btnDownload_Click(object sender, EventArgs e)
    {
    try
        {
            string strRequest = "Quiz.aspx.cs";
            string path = Server.MapPath(strRequest); //get absolute path of the file
            System.IO.FileInfo file = new System.IO.FileInfo(path); //get file object as FileInfo
            if (file.Exists) //-- if the file exists on the server
                {
                Response.Clear(); //set appropriate headers
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/....";
                Response.WriteFile(file.FullName);
                Response.End(); //if file does not exist
                }
                else
                {
                Response.Write("This file does not exist.");
                } //nothing in the URL as HTTP GET
}

    catch (Exception rt)
        {
          Response.Write(rt.Message);
        }
   }

it works fine but the problem is that when the user clicks on download button, the page is refreshed. As a result all the Javascript modifications to the page are lost. Is there any way to do it without refreshing the page?

Author

Commented:
I was trying to do this (works in simple HTML page)
<a href="#" onclick="javascript:document.execCommand('SaveAs','1','C:\sample2.mp3')">save</a>

so I wrote the following in my ASP page, but it gives an error. Seems I have not written ASP correctly

cell_Flash_btn_Transaction.InnerHtml = "<a href='#' onclick='javascript:document.execCommand(/'SaveAs/',/'1/',/'C:\\sample2.mp3/')><img src='../images/download.JPG' border=0></a>";

Please help

Author

Commented:
I tried another approach

1. in my aspx page, wrote the following
<%
       String url = "http://mydoamin.com:80/public/preview/090099b680d328fc/Section1.mp3";
     %>
    <div>
    <a href="#" onClick="window.open('Test.aspx?fileName=<%= url %>','mywindow','width=400,height=200')">Download MP3</a>
    </div>
   

2. In Test.aspx.cs (inside Page_Load function) did the following (code snippet attached)
This works fine if the file is on the same web server but in our case the mp3 file is being fetched from another url. Please tell me how to resolve this problem.


protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string strRequest = Request.QueryString["fileName"].ToString();
            
            string path = Server.MapPath(strRequest); 
            System.IO.FileInfo file = new System.IO.FileInfo(path); 
            if (file.Exists) 
            {
                Response.Clear(); 
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/....";
                Response.WriteFile(file.FullName);
                Response.End(); 
            }
            else
            {
                Response.Write("This file does not exist.");
            } 
        }
 
        catch (Exception rt)
        {
            Response.Write(rt.Message);
        }
    }
   

Open in new window

Author

Commented:
If I directly paste the URL in the browser it plays fine. So that means access to the file is now a problem.

Author

Commented:
Sorry, spelling mistake above.

"If I directly paste the URL in the browser it plays fine. So that means access to the file is NOT a problem."

Author

Commented:
Any one please
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.