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
coerraceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
in file2.asp, try encoding the file name before trying to open it:

fname=Server.URLEncode( objfile.name )
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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
0
coerraceAuthor Commented:
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
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
what is rendered for the file name when you use that code?
0
coerraceAuthor Commented:
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
0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
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 )%>'); }
0
coerraceAuthor Commented:
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
0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
change it to:

window.open('file2.asp?path=../tests/Upload2/Userfiles/<%=Server.URLEncode( session("cn"))%>'); }
0
coerraceAuthor Commented:
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
0
coerraceAuthor Commented:
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
0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
you had the code right, the issue is the encoding on the page is incorrect.

try this:

<%
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
coerraceAuthor Commented:
the origin of all is this line if you enter in the browser:

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

This is the origin.
0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
correct, the value

testí.doc

has special chars that need to be encoded in order for the server to understand them.
0
coerraceAuthor Commented:
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?
0
coerraceAuthor Commented:
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
0
coerraceAuthor Commented:
Thank you
0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
was just about to respond to suggest just that!

glad you got it resolved :)
0
coerraceAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.