Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Downloading a file without opening.

Status
Not open for further replies.

nsarun

IS-IT--Management
Nov 5, 2003
6
US
Dear Wizkids,

Need your help once again. I would like to get a 'Open Save ...' dialog box when the user clicks the link on my page that points to a file of known MIME type (such as .doc, .xls or .pdf etc.). I do not want the relevant application(such as Word, Excel, PPT etc.) to be opened to show the document. I dont want to keep the file without an extension on the server.

Can you please help me on this.

Thanks
Amilie
 
I don't know that JavaScript can do what you're asking. You might have better luck doing this with a server-side process like ASP.NET.
Code:
	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim strRequest As String = Request.QueryString("file")
		If strRequest <> "" Then
			'Replace anything before the file name with our special download folder
			'so hackers can't use this script to download any file they want.
			strRequest = "DownloadFiles/" & Regex.Match(strRequest, "[^\\|/]*$").Value.Trim()

			Dim file As New System.IO.FileInfo(Server.MapPath(strRequest))
			If file.Exists Then
				Response.Clear()
				Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
				Response.AddHeader("Content-Length", file.Length.ToString())
				Response.ContentType = "application/octet-stream"
				Response.WriteFile(file.FullName)
				Response.End()
			Else
				Response.Write("This file does not exist.")
			End If
		Else
			Response.Write("Please provide a file to download.")
		End If
	End Sub

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top