Solved

prevent browser to open text and pdf files..

Posted on 2006-07-24
4
350 Views
Last Modified: 2012-06-27
Hello

I want to know if there is a way to force web browsers to open download text and pdf documents intsead of opening it there only..

Basically, i want my web users to download pdf and text files intead opening it in the borwser irrespective of their local browser settings.. Is there a way to force browser in this way programatically ??

Thank You

Neeraj
0
Comment
Question by:neeraj523
4 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17166044
Hi neeraj523,

I've seen this question asked many a time and this is the only thread i have seen with a reasonable response:

http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20811501.html

Apresto
0
 
LVL 6

Accepted Solution

by:
darkeryu earned 500 total points
ID: 17166603
HI:

using asp you can:

<%
  snfile="/foldername/filename"
  set fs=server.CreateObject("scripting.filesystemobject")
  if fs.FileExists(server.MapPath(snfile))=true then
    set objfile=fs.GetFile(server.MapPath(snfile))
    size=objfile.size
    fname=objfile.name
    mime_type="application/save-as"
    Response.AddHeader "Expires:",0
    Response.ContentType=mime_type
    Response.AddHeader "Content-Disposition","attachment; filename="&fname
    set objStream =server.CreateObject("ADODB.Stream")
    objStream.Open
    objStream.Type=1
    objStream.LoadFromFile server.MapPath(snfile)
    Response.BinaryWrite objStream.Read
  else
    Response.Write "File not found!"
    Response.End
  end if
 
%>
0
 
LVL 22

Author Comment

by:neeraj523
ID: 17166633
Hello darkeryu

I have tried solution given by u.. but not got working fine.. It is opening the download window but gives error that specific server is not not found..

Neeraj
0
 
LVL 10

Expert Comment

by:eguilherme
ID: 17191954
<%
Function ReadBinaryFile(FileName)
  Const adTypeBinary = 1
  Dim BinaryStream
  Set BinaryStream = CreateObject("ADODB.Stream")
  BinaryStream.Type = adTypeBinary
  BinaryStream.Open
  BinaryStream.LoadFromFile FileName
  ReadBinaryFile = BinaryStream.read
end function


Dim fso, f1, Arq, Nome, Ext
Arq = Server.MapPath("test.txt") 'here u specify the location of the file
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.GetFile(Arq)
Nome = fso.GetFileName(Arq)
Ext = fso.GetExtensionName(Arq)
Response.Clear()
Response.ContentType = "application/"&Ext
response.AddHeader "Content-Disposition", "attachment; filename="&Nome&""      
response.BinaryWrite(ReadBinaryFile(Arq))
Response.Flush()
%>
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 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