Link to home
Start Free TrialLog in
Avatar of coerrace
coerrace

asked on

download PDFs vb script without embed

Hello I have this code in a file called "file2.asp" and is to download a file and avoid the browsers embed the PDFs with their default PDF viewer:


<%
  snfile=Request.QueryString("path")
  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
  
%>

Open in new window


I call that file from other file called "main.asp" that has this code:


<HTML>
<HEAD>
<!--<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">-->
<META http-equiv="Pragma" CONTENT="no-cache">
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
 <meta charset="utf-8">
<TITLE>Support</TITLE>
<style type="text/css">
.buttonback1{box-shadow:black 1px 1px 3px;border-radius: 4px;cursor:pointer;height:44px;margin-bottom:4px;margin-top:4px;margin-left:4px;margin-right:4px;border:0px solid #622F00;
background: rgb(61,155,226); 
background: -moz-linear-gradient(top,  rgba(61,155,226,1) 0%, rgba(40,92,155,1) 100%); 
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(61,155,226,1)), color-stop(100%,rgba(40,92,155,1))); 
background: -webkit-linear-gradient(top,  rgba(61,155,226,1) 0%,rgba(40,92,155,1) 100%); 
background: -o-linear-gradient(top,  rgba(61,155,226,1) 0%,rgba(40,92,155,1) 100%); 
background: -ms-linear-gradient(top,  rgba(61,155,226,1) 0%,rgba(40,92,155,1) 100%); 
background: linear-gradient(to bottom,  rgba(61,155,226,1) 0%,rgba(40,92,155,1) 100%); 
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#3d9be2', endColorstr='#285c9b',GradientType=0 ); 
}
.buttonback1o{box-shadow:black 1px 1px 3px;border-radius: 4px;cursor:pointer;height:44px;margin-bottom:4px;margin-top:4px;margin-left:4px;margin-right:4px;border:0px solid #622F00;background: rgb(145,145,145); 
background: -moz-linear-gradient(-45deg,  rgba(145,145,145,1) 0%, rgba(0,0,0,1) 100%); 
background: -webkit-gradient(linear, left top, right bottom, color-stop(0%,rgba(145,145,145,1)), color-stop(100%,rgba(0,0,0,1))); 
background: -webkit-linear-gradient(-45deg,  rgba(145,145,145,1) 0%,rgba(0,0,0,1) 100%); 
background: -o-linear-gradient(-45deg,  rgba(145,145,145,1) 0%,rgba(0,0,0,1) 100%); 
background: -ms-linear-gradient(-45deg,  rgba(145,145,145,1) 0%,rgba(0,0,0,1) 100%); 
background: linear-gradient(135deg,  rgba(145,145,145,1) 0%,rgba(0,0,0,1) 100%); 
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#919191', endColorstr='#000000',GradientType=1 ); 
}
.buttontext{font-family: arial;text-shadow:black 0px 0px 0px;margin-left:15px;margin-right:15px;vertical-align:middle;cursor:pointer;height:44px;line-height:44px;color:white;font-size:100%;font-weight:bold;}
.buttontext2{font-family: arial;text-shadow:black 0px 0px 0px;margin-left:15px;margin-right:15px;vertical-align:middle;cursor:pointer;height:44px;color:black;font-size:100%;font-weight:bold;}
</style>
</head>
<body> 
<SCRIPT>
	
</script>






<SCRIPT>





function download()
{
    window.open('file2.asp?path=../tests/Upload2/Userfiles/call.pdf'); } 

</SCRIPT> 

<SPAN onmouseup=download(); style='cursor:pointer;display:inline-block;white-space:nowrap;'><SPAN><TABLE onmouseover='this.className="buttonback1o"' onmouseout='this.className="buttonback1"'; class='buttonback1' cellspacing=0 cellpadding=0 height=100%;'><TH align=center valign=middle><SPAN style='white-space:nowrap;'><P class=buttontext>Download PDF</TH></TABLE></CENTER></P></SPAN></SPAN></P>

</SCRIPT>

</body>
</html> 

Open in new window



   Then the file to download there is call.pdf and comes from this line in the code:

 window.open('file2.asp?path=../tests/Upload2/Userfiles/call.pdf'); } 

Open in new window


   Now in that line if I use files with special characters in their names like testí.pdf or filó.pdf or pár.pdf or öfi.pdf or Óme.pdf or Áfg.pdf and so on with any special character in the filename say the "file2.asp" not found and is there the file inside the directory and it just happens with special characters in name of a file for other files without special characters works perfect.
   Anyone know what to add or fix in the code to make able to run with special characters also?

Thank you
Avatar of Big Monty
Big Monty
Flag of United States of America image

in file2.asp, try encoding the file name before trying to open it:

fname=Server.URLEncode( objfile.name )
I think you need to use non special characters.   http://support.microsoft.com/kb/177506

You can use a-Z, 0-9 and this list
   ^   Accent circumflex (caret)
   &   Ampersand
   '   Apostrophe (single quotation mark)
   @   At sign
   {   Brace left
   }   Brace right
   [   Bracket opening
   ]   Bracket closing
   ,   Comma
   $   Dollar sign
   =   Equal sign
   !   Exclamation point
   -   Hyphen
   #   Number sign
   (   Parenthesis opening
   )   Parenthesis closing
   %   Percent
   .   Period
   +   Plus
   ~   Tilde
   _   Underscore
Avatar of coerrace
coerrace

ASKER

Big Monty:   I changed the "file2.asp" code like:

<%
  snfile=Request.QueryString("path")
  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=Server.URLEncode( 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
  
%>

Open in new window


   With  "fname=Server.URLEncode( objfile.name )" and does the same not results any other thing to add or remove to code?

Scot Fell (padas:) special characters are required in the files
Thank you
what is rendered for the file name when you use that code?
I use for example these files:

testí.pdf
 filó.pdf
 pár.pdf
öfi.pdf
 Óme.pdf
 Áfg.pdf

And one example could be of how I call using the code above described:

 window.open('file2.asp?path=../tests/Upload2/Userfiles/testí.pdf'); }

Thank you
how are you populating the window.open line? I assume that's from a server side variable, something like:

 window.open('file2.asp?path=<%=someFileName%>'); }

if so, try using the encoding method there:

 window.open('file2.asp?path=<%=Server.URLEncode( someFileName )%>'); }
We use a variable session but we changed the line of code to look like this:

window.open('file2.asp?path=../tests/Upload2/Userfiles/' + <%=Server.URLEncode( session("cn"))%>); }

   And the button stop work is dead.
   The variable session comes in this way:

filenamecomplete = Request.QueryString("filenamecomplete")
Session("cn") = filenamecomplete

Thank you
change it to:

window.open('file2.asp?path=../tests/Upload2/Userfiles/<%=Server.URLEncode( session("cn"))%>'); }
I have this sequence now:
<%
filenamecomplete = Request.QueryString("filenamecomplete")
Session("cn") = Server.URLEncode(filenamecomplete)
%>

<SCRIPT>

var launch = '<%= session("cn") %>';


function download()
{
    window.open('file2.asp?path=../tests/Upload2/Userfiles/' + launch); }
</SCRIPT>

and nothing it works the button but same error not found.
Could you mod the code at the above to make work with one example, maybe there is something wrong.
Thank you
I used like you say also like this:

 window.open('file2.asp?path=../tests/Upload2/Userfiles/<%=Server.URLEncode( session("cn"))%>');

and give same results not found and in the browser where you enter a www to navigate say something similar to:

myip/file2.asp?path=../tests/Upload2/Userfiles/testí%2Edoc
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the origin of all is this line if you enter in the browser:

myip/doc.asp?filenamecomplete=testí.doc

This is the origin.
correct, the value

testí.doc

has special chars that need to be encoded in order for the server to understand them.
I used like you said:

<%
Response.CodePage = 65001    
Response.CharSet = "utf-8"

filenamecomplete = Request.QueryString("filenamecomplete")
Session("cn") = Server.URLEncode(filenamecomplete)
%>

<SCRIPT>

var launch = '<%= session("cn") %>';


function download()
{
    window.open('file2.asp?path=../tests/Upload2/Userfiles/' + launch); } 
</SCRIPT>

Open in new window


and nothing file not found and in the www appears the same:

file2.asp?path=../tests/Upload2/Userfiles/testí%2Edoc

 What could be the problem? Did you check from your side?
I could solved adding the 2 lines:

Response.CodePage = 65001    
Response.CharSet = "utf-8"

in file2.asp also not just in the main file.
Thank you
Thank you
was just about to respond to suggest just that!

glad you got it resolved :)
I checked and the Server.URLEncode() is not required. Is just required the lines:

Response.CodePage = 65001    
Response.CharSet = "utf-8"

With the 2 lines on file2.asp and nothing else works, anyway I wrote the lines also to the main page that call file2.asp just in case although work all only with that 2 lines in file2.asp
Thank you