Solved

Force Download

Posted on 2008-10-22
3
219 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

6 Experts available now in Live!

Get 1:1 Help Now