Solved

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

Posted on 2014-02-20
4
613 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
[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
  • 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Find out what you should include to make the best professional email signature for your organization.
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 the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

752 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