Solved

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

Posted on 2004-09-02
5
1,525 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

630 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