I'm creating a web page that will allow user to upload files to the server.
I was able to save the file on the server, but I notice that the office files (e.g. word, excel) are corrupted and can not be opened.
My UI is fairly simple
<form method="post" enctype="multipart/form-data" action="uploadFile.asp">
<p>Select a file:<br><input type=File size=30 name="file1"></p>
<input type=submit value="Upload">
</form>
In my uploadFile.asp
, I'm using VBScript. I tried to read and write the binary data and write it directly.
Function SaveBinaryData(FileName, ByteArray)
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save binary data.
BinaryStream.Type = adTypeBinary
'Open the stream And write binary data To the object
BinaryStream.Open
BinaryStream.Write ByteArray
'Save binary data To disk
BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
End Function
Dim biData
biData = Request.BinaryRead(Request.TotalBytes)
SaveBinaryData "C:\Uploads\ww.xlsx", biData
Like I mentioned previously, if I upload a excel or word file, the file is corrupted. However, text files will work just fine.
I tried other solutions I found online such as Pure ASP
, ShadowUploader
, etc, but couldn't find one that work properly, they all result in corrupt file or doesn't upload at all.
How can I get it to work properly so that I can upload binary files such as microsft word or excel?
Any help is appreciated!
I don't see how your code would work.
Classic ASP has no way accessing uploaded files like ASP.NET (Request.UploadedFiles) so if you don't use a a COM component then you need to read Request.BinaryStream and parse out the contents, which isn't so easy.
There are several Classic ASP scripts that do this and I would suggest you use one of these. I have used several and haven't had any problems. I suggest you try one of the free ones like: http://freevbcode.com/ShowCode.asp?ID=4596