File uploading / objFile.Write problem
I am trying to improve an uploading script.
The script read the post (using BinaryRead), put each item's attribute (name,
value, content type etc...) into a scripting dictionary and then put each
item scripting dictionary into another scripting dictionary. Then, one can
create a file object on the server and write the content (value) of uploaded
file to it.
(Here is the upload portion:
value = UploadRequest.Item("blob").Item("Value") '-- Get the content of the
file
'Create FileSytemObject Component '
Set ScriptObject = Server.CreateObject("Scripting.FileSystemObject")
'Create and Write to a File '
fileextn = lcase(right(filename,4)) '--Extart the file extention
Set MyFile = ScriptObject.CreateTextFile(strpiclocation) '--Create a file
object in the specified folder
If fileextn=".jpg" Then '--Check to see that the file is of the right type
(Can't use content-type???)
'--Isn't it better to do this check before creating FileSystemObject and
TextFile? Save server resources...)
blnOK = True '--Set flag to true
For i = 1 to LenB(value) '--Write the Binary data (content of file) to the
file on the server
myVal = MidB(value,i,1)
MyFile.Write chr(AscB(myVal))
Next
Else '--File of type the isn't allowed to be uploaded
blnOK = False '--Set flag to false
End If
MyFile.Close '--Close the file
The script worked well and then I wanted to make it more generic by writing
a function for uploading:
'--Do the actual loading of the content of the file.
'--strFileName - uploaded file name
'--strFileContent - the content of the file to be saved
'--strPicLocation - where the file should be saved
'--strAllowedTypes - delimited string of allowed file types
Function DoUpload(strFileName, strFileContent, strPicLocation, strAllowedTypes)
'--Extart the file extention
fileextn = lcase(right(filename,4))
'Response.Write strFileContent
If InStr(1, strAllowedTypes, fileextn, 1) <> 0 Then '--Check to see if the
file is of allowed type (case insensative)
'fileextn=".jpg" Then (old)
'--Create Instance of FileSytemObject Component
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'-- Create a file to write to in the specified directory
Set objFile = objFSO.CreateTextFile(strPicLocation)
If objFSO.FileExists(strPicLocation) Then
'--Write the Binary data (content of file) to the file on the server
Response.Write "Writing to File"
For i = 1 to LenB(value)
myVal = MidB(value,i,1)
myVal = AscB(myVal)
objFile.Write(chr(myVal))
Next
Else
Response.Write "UNable to create file"
End If
'--Close the file
MyFile.Close
Set objFSO = Nothing
DoUpload = True '--Set return value to true
Else '--File of type the isn't allowed to be uploaded
DoUpload = False '--Set return value to false
End If
End Function
I then call it with the same values from the original script and get an error:
"Error Type:
Microsoft VBScript runtime (0x800A0005)
Invalid procedure call or argument
/salem/upload/do_upload.asp, line 252"
Line 252 is the one "objFile.Write(chr(myVal))"
Any Ideas what is going on? Is there a problem passing the binary data to
the function? (Goes through some conversion or something???)
MAny thanks
Tomer
P.S.
I had similar problem before and then I changed:
objFile.Write(AscB(MidB(value,i,1)))
to:
myVal = MidB(value,i,1)
objFile.Write(AscB(myVal))
...
Re: File uploading / objFile.Write problem
After additional debugging I found out that if I am able to upload text file.
But when trying to upload .jpg I get that "Invalid procedure call or argument"
error on the line of the "MyFile.Write chr(AscB(myValue))".
Further more, if I instead of writing the .jpg to the file I write it to
the screen (Change MyFile.Write to Response.Write) I get a bunch og jibrish
printed on the screen (The binary content...)
This stupid little mistake (I know it will be something like that) is holding
me from continouing the project. so please, someone, HELPPPPPPPPPPPPPPP!!!!!!!!!!