Solved

Force Download

Posted on 2008-10-22
3
220 Views
Last Modified: 2013-11-05
Is there anyway to have a link that will force the browser to download regardless of how the mime is setup in their browser?  I want to let people download mp3 files without have to right-click and save as.
0
Comment
Question by:kyleroi
  • 2
3 Comments
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 22784601
save the below code as down.aspx.

call this file and pass one parameter specifying the name of file.

e.x.

response.redirect("down.aspx?file=" & filenamefromVariable)

This will force the file under the "upload directory" to be downloaded

<%@ Page language="vb" runat="server" explicit="true" strict="true" %>

<script language="vb" runat="server">

Sub Page_Load(Sender As Object, E As EventArgs)

	Dim filenames As String = Request.QueryString("file")

	'-- if something was passed to the file querystring

	If filenames <> "" Then

		Dim strRequest As String = "./upload/" & Request.QueryString("file")

		Dim fileNameasSave As String = Request.QueryString("name")

		'get absolute path of the file

		Try

			Dim path As String = Server.MapPath(strRequest)

			'get file object as FileInfo

			Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)

			'-- if the file exists on the server

			If file.Exists Then

				'set appropriate headers

				Response.Clear()

				Response.AddHeader("Content-Disposition", "attachment; filename=" & fileNameasSave)

				Response.AddHeader("Content-Length", file.Length.ToString())

				Response.ContentType = "application/octet-stream"

				Response.WriteFile(file.FullName)

				Response.End

			'if file does not exist

			Else

				Response.Write("This file does not exist.")

			End If

			'nothing in the URL as HTTP GET

		Catch ex As Exception

			Response.Write(ex.Message)

		End Try

	Else

		Response.Write("Please provide a file to download.")

	End If

End Sub

</script>

Open in new window

0
 
LVL 1

Author Comment

by:kyleroi
ID: 22789323
My apologies, I was not clear enough.  I want to force the download (or prompt to save file) using a file on a different server?  Is this possible?  If not I guess I could put the down.aspx on that server.
0
 
LVL 18

Accepted Solution

by:
Ajay Sharma earned 500 total points
ID: 22793706
i think, without using any server-side code its nearly impossible as the content is originated from the server, and if you don't have access to server resources then how can u control the data (response) coming from that server ??

Logically u should have some access to the server.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adjust the position 3 49
Runtime Error 2 28
Entity Framework 7 31
Importing CSV file encoded in UTF-8 in SQL SERVER 2008 using BULK INSERT 5 28
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

22 Experts available now in Live!

Get 1:1 Help Now