Solved

Force download when clicked (Save Target As...)

Posted on 2004-09-02
5
1,520 Views
Last Modified: 2011-10-03
I would like to know of a script that forces a .mp3 or .wma, .rm file to download no matter what the users has set as a default for this file.  Same as if the user was to select the right click option:
"Save Target As..."

Example,
http://www.hydrozeen.com/Hydrozeen-Pile%20of%20Bones.mp3

Normally when you click on the basic link your computer settings will either stream this into your system or ask to download it.

Now you can always right click and then select:
Save Target As...
and this is what I would like the default for the link to be, so how do I change a basic .mp3 link into a force download link?

Thanks for the answer,
Stephen

0
Comment
Question by:lnternet
  • 3
5 Comments
 
LVL 15

Expert Comment

by:justinbillig
ID: 11964428
heres an example but it's ASP

http://www.psacake.com/web/if.asp
0
 
LVL 15

Expert Comment

by:justinbillig
ID: 11964437
when the user clicks the link you could submit a download form to a new window then use that script on that page
0
 
LVL 3

Accepted Solution

by:
Ayesha_K earned 500 total points
ID: 11964555
Hi,

I'm using the following function in ASP.NET to force the IE to download my file and not start opening it in its specified format.

    Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs)
        On Error GoTo errHandler
        Dim strPhysicalPath As String
        Dim objFileInfo As System.IO.FileInfo
        Dim ErrMsg As String

        strPhysicalPath = Server.MapPath(Session("MyFileName"))

        'exit if file does not exist
        If Not System.IO.File.Exists(strPhysicalPath) Then Exit Sub
        objFileInfo = New System.IO.FileInfo(strPhysicalPath)
        Response.Clear()

        Response.ClearHeaders()
        Response.Clear()
        Response.ContentType = "text/plain"
        Response.Charset = ""
        Response.AddHeader("Content-Disposition", "attachment;filename=" + objFileInfo.Name)
        Response.AddHeader("Content-Length", objFileInfo.Length.ToString())
        Response.WriteFile(strPhysicalPath)
        Response.Flush()
        Response.Close()
        Exit Sub

errHandler:
        ErrMsg = Err.Description
    End Sub
0
 
LVL 15

Expert Comment

by:justinbillig
ID: 12605959
I think the points should be split between myself and Ayesha_K since both are viable options
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question