Solved

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

Posted on 2004-09-02
5
1,517 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now