Solved

forcing file to open in another page or prompt to save or open

Posted on 2014-02-20
4
593 Views
Last Modified: 2014-02-22
I have the following code which gets the file location through sql to the file on the physical web server

<!--#include file="include/dbcommon.asp"-->
<!--#include file="include/OrderImage_variables.asp"-->
<%
if SESSION("UserID")="" or not CheckSecurity(SESSION("_" & strTableName & "_OwnerID"),"Search")then
	response.Redirect "login.asp"
	response.End
end if

Response.Expires = 0
Response.Buffer = True
Response.Clear

strFilename=REQUEST.querystring("filename")
ext=mid(strFilename,len(strFilename)-4)

select case ext
	case ".asf"	ctype = "video/x-ms-asf"
	case ".avi" ctype = "video/avi"
	case ".doc" ctype = "application/msword"
	case ".zip" ctype = "application/zip"
	case ".xls" ctype = "application/vnd.ms-excel"
	case ".gif" ctype = "image/gif"
	case ".jpg" ctype = "image/jpeg"
	case "jpeg" ctype = "image/jpeg"
	case ".wav" ctype = "audio/wav"
	case ".mp3" ctype = "audio/mpeg3"
	case ".mpg" ctype = "video/mpeg"
	case "mpeg" ctype = "video/mpeg"
	case ".rtf" ctype = "application/rtf"
	case ".htm" ctype = "text/html"
	case "html" ctype = "text/html"
	case ".asp" ctype = "text/asp"
	case else ctype = "application/octet-stream"
end select

field = REQUEST.querystring("field")
if not CheckFieldPermissions(field,"") then response.End

'//	construct sql

set keys = CreateObject("Scripting.Dictionary")
keys.Add ("ImageID"),postvalue("key1")
where=KeyWhere(keys,"")


dbConnection=""
db_connect()
Set rs = server.CreateObject("ADODB.Recordset")


sql = gSQLWhereAdd(where)

rs.Open sql,dbConnection, 1,2

if rs.EOF then
	rs.Close
	response.Redirect "login.asp"
	response.End
end if
'----------------------------------------------------------------
Const adTypeBinary = 1
  
  'Create Stream object
  Dim BinaryStream
  Set BinaryStream = CreateObject("ADODB.Stream")
  
  'Specify stream type - we want To get binary data.
  BinaryStream.Type = adTypeBinary
  
  'Open the stream
  BinaryStream.Open
  
  'Load the file data from disk To stream object
  'BinaryStream.LoadFromFile FileName
    BinaryStream.LoadFromFile strFilename 
  'Open the stream And get binary data from the object
 ' ReadBinaryFile = BinaryStream.Read

binTemp = BinaryStream.Read





'-------------------------------------------------------------------

if IsNull(binTemp) then
	response.end
end if

Response.ContentType = ContentType
Response.AddHeader "Content-Disposition", "attachment;Filename=""" & strFileName & """"
Response.AddHeader "Content-Length", lenb(db_stripslashesbinary(binTemp))


Response.BinaryWrite binTemp


rs.Close
response.End

%>

Open in new window


using different browsers like safari it downloads the file and other browsers such as ie will prompt to save or open the file.  I want to force it always to prompt to save or open unless somehow I can force it to open in another page.  all files are pdf's
0
Comment
Question by:rmardis
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39876505
Just set the content type to

application/octet-stream

and the file will prompt
0
 

Author Comment

by:rmardis
ID: 39879376
in my code where would this change go?
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 39879530
Well I would have said replace your
select case ext
	case ".asf"	ctype = "video/x-ms-asf"
	case ".avi" ctype = "video/avi"
	case ".doc" ctype = "application/msword"
	case ".zip" ctype = "application/zip"
	case ".xls" ctype = "application/vnd.ms-excel"
	case ".gif" ctype = "image/gif"
	case ".jpg" ctype = "image/jpeg"
	case "jpeg" ctype = "image/jpeg"
	case ".wav" ctype = "audio/wav"
	case ".mp3" ctype = "audio/mpeg3"
	case ".mpg" ctype = "video/mpeg"
	case "mpeg" ctype = "video/mpeg"
	case ".rtf" ctype = "application/rtf"
	case ".htm" ctype = "text/html"
	case "html" ctype = "text/html"
	case ".asp" ctype = "text/asp"
	case else ctype = "application/octet-stream"
end select

Open in new window


but you never use it so remove the above and change

Response.ContentType = ContentType

to

Response.ContentType = "application/octet-stream"
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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