Click to See Complete Forum and Search --> : Upload PDF file using windows VBS
checksal
February 4th, 2005, 05:16 PM
Hi. I am trying to load a PDF file using the following code:
Set objSourceFile = objFSO.OpenTextFile(SendFile,1,False )
Do Until objSourceFile.AtEndOfStream
xfile = xfile & Asc(objSourceFile.Read(1)) & ","
Loop
objSourceFile.Close
Then xFile is sent over to the server using a Microsoft.XMLHTTP and it is apparently saved as a pdf file. When I try to open the file I get "Adobe reader could not open file because it is either not a supported file type or because the file has been corrupted". Please help. I am new and I do not know what else to do!
j0nas
February 4th, 2005, 05:42 PM
OpenTextFile might not be the correct way of sending binary files.
I don't know anything about scriting (just happen to see this thread), so don't ask me how to open it in binary mode (but I guess there is a method for that as well).
You can also diffing the files with for instance the FC.EXE program (in your system32 folder). Before you do that, compare the file sizes... I bet they differ in size since extra CL-LF translation has been added to the uploaded file.
Hope it helps.
checksal
February 7th, 2005, 12:55 PM
I have changed the code to the following:
Const adTypeText = 2
Dim BinaryStream 'As New Stream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = adTypeBinary
BinaryStream.Open
BinaryStream.LoadFromFile File1
BinaryStream.Position = 0
BinaryStream.Type = adTypeText
BinaryStream.CharSet = "us-ascii"
Output = BinaryStream.ReadText
Now it works for atleast some pdf files. For others I just get partial read and it tells me the files are corrupt when I try to open them on the server side. Also if I try to save them before I do the BinaryStream.ReadText it saves the file properly. I am truly at loss here. HELP PLEASE!
j0nas
February 7th, 2005, 02:51 PM
BinaryStream.Type = adTypeText
BinaryStream.CharSet = "us-ascii"
This can't be right... You first set BinaryStream.Type to binary, then the Type-property is overwritten in above lines.
checksal
February 7th, 2005, 04:23 PM
Hi j0nas,
Yes if I do not reset if back the type 2 then I get an error message saying 'operation not allowed in this context' referring to this line
BinaryStream.CharSet = "us-ascii". See if it stays binary then I can't send it along with other text and that is my problem. I need to convert it to binary then convert it where it is able to be send along with other text (XML) and then on the server side reconvert it back to be able to be saved. The problem is that it is not even being red properly on the client side. :( I got the following code from http://www.eggheadcafe.com/articles/20010829.asp :
function getFileBytes(flnm, sType)
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
if sType="on" then
objStream.Type = 1 ' adTypeBinary
else
objStream.Type = 2 ' adTypeText
objStream.Charset ="ascii"
end if
objStream.Open
objStream.LoadFromFile flnm
if sType="on" then
getFileBytes=objStream.Read
else
getFileBytes= objStream.ReadText
end if
objStream.Close
Set objStream = Nothing
end function
This person also uses the same code that is used by me. Maybe you can see something I cant? THANKS!
checksal
February 7th, 2005, 04:45 PM
Ok I did what you had said earlier. About commenting out one of the types. Now I use only type 2. Here is my code:
Function ReadPDFFile(FileName)
Const adTypeText = 2
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = adTypeText 'adTypeBinary
BinaryStream.CharSet = "us-ascii"
BinaryStream.Open
BinaryStream.Position = 0
BinaryStream.LoadFromFile FileName
ReadTextFile = BinaryStream.ReadText
End Function
This is the error msg I get when I try to open the file on the server side:
"adobe reader could not open the pdf file because it is either not a supported file type or it has been corrupted."
When I used to convert it from binary to text like previously, I used to only get the error that file is missing data. Thanks.
checksal
February 8th, 2005, 11:31 AM
Ok I got it to where I can now open the PDF files. But the files are empty. What I am doing is converting the PDF file to base64 on vbs. The following is the code used on the webservice.
//fileData is the base64 coming from the VBS
byte[] bb = System.Convert.FromBase64String(fileData);
System.IO.MemoryStream bs = new System.IO.MemoryStream(bb);
FileStream fs = new FileStream(@"C:\"+filename,FileMode.OpenOrCreate,FileAccess.Write);
fs.Close();
Please help because it is hard for a newbie like me. I do not know why the files are empty. Its baffling.
checksal
February 8th, 2005, 04:51 PM
Solved the issue. It was much more complex than what I was doing earlier. Thanks for everyone who helped.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.