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:
I call that file from other file called "main.asp" that has this code:
Then the file to download there is call.pdf and comes from this line in the code:
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
<%
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
%>
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>
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'); }
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
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
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
ASKER
Big Monty: I changed the "file2.asp" code like:
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
<%
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
%>
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?
ASKER
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?pat h=../tests /Upload2/U serfiles/t estí.pdf') ; }
Thank you
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?pat
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?pat h=<%=someF ileName%>' ); }
if so, try using the encoding method there:
window.open('file2.asp?pat h=<%=Serve r.URLEncod e( someFileName )%>'); }
window.open('file2.asp?pat
if so, try using the encoding method there:
window.open('file2.asp?pat
ASKER
We use a variable session but we changed the line of code to look like this:
window.open('file2.asp?pat h=../tests /Upload2/U serfiles/' + <%=Server.URLEncode( session("cn"))%>); }
And the button stop work is dead.
The variable session comes in this way:
filenamecomplete = Request.QueryString("filen amecomplet e")
Session("cn") = filenamecomplete
Thank you
window.open('file2.asp?pat
And the button stop work is dead.
The variable session comes in this way:
filenamecomplete = Request.QueryString("filen
Session("cn") = filenamecomplete
Thank you
change it to:
window.open('file2.asp?pat h=../tests /Upload2/U serfiles/< %=Server.U RLEncode( session("cn"))%>'); }
window.open('file2.asp?pat
ASKER
I have this sequence now:
<%
filenamecomplete = Request.QueryString("filen amecomplet e")
Session("cn") = Server.URLEncode(filenamec omplete)
%>
<SCRIPT>
var launch = '<%= session("cn") %>';
function download()
{
window.open('file2.asp?pat h=../tests /Upload2/U serfiles/' + 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
<%
filenamecomplete = Request.QueryString("filen
Session("cn") = Server.URLEncode(filenamec
%>
<SCRIPT>
var launch = '<%= session("cn") %>';
function download()
{
window.open('file2.asp?pat
</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
ASKER
I used like you say also like this:
window.open('file2.asp?pat h=../tests /Upload2/U serfiles/< %=Server.U RLEncode( 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=../tes ts/Upload2 /Userfiles /testí%2Ed oc
window.open('file2.asp?pat
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=../tes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
the origin of all is this line if you enter in the browser:
myip/doc.asp?filenamecompl ete=testí. doc
This is the origin.
myip/doc.asp?filenamecompl
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.
testí.doc
has special chars that need to be encoded in order for the server to understand them.
ASKER
I used like you said:
and nothing file not found and in the www appears the same:
file2.asp?path=../tests/Up load2/User files/test í%2Edoc
What could be the problem? Did you check from your side?
<%
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>
and nothing file not found and in the www appears the same:
file2.asp?path=../tests/Up
What could be the problem? Did you check from your side?
ASKER
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
Response.CodePage = 65001
Response.CharSet = "utf-8"
in file2.asp also not just in the main file.
Thank you
ASKER
Thank you
was just about to respond to suggest just that!
glad you got it resolved :)
glad you got it resolved :)
ASKER
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
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
fname=Server.URLEncode( objfile.name )