读取POSTed二进制文件并写入新的二进制文件

时间:2015-06-11 03:03:38

标签: excel vbscript asp-classic

我正在创建一个允许用户将文件上传到服务器的网页。

我能够将文件保存在服务器上,但我注意到office文件(例如word,excel)已损坏且无法打开。

我的用户界面非常简单

angular.module('DroidRestClient.tabsPageController', ['ionic'])
    .controller('TabsPageController', ['$scope', '$state', function ($scope, $state) {
            $scope.navTitle = 'Home';

            $scope.leftButtons = [{
                    type: 'button-icon icon ion-navicon',
                    tap: function (e) {
                        $scope.toggleMenu();
                    }
                }];
        }]);

在我的<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> 中,我使用的是VBScript。我试图读取和写入二进制数据并直接写入。

uploadFile.asp

就像我之前提到的,如果我上传excel或word文件,该文件已损坏。但是,文本文件可以正常工作。

我尝试了我在网上找到的其他解决方案,例如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 Pure ASP等,但无法找到一个正常工作的解决方案,它们都会导致文件损坏或者没有完全上传。

如何让它正常工作以便我可以上传二维文件,例如microsft word或excel?

感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

表格

<form method="post" action="post.asp" enctype="multipart/form-data">
<input type='file' name='blob' size='80' />
</form>

然后是post.asp的代码。首先是asp二进制代码:

Dim folder
folder = "public"

Response.Expires=0
Response.Buffer = TRUE
Response.Clear

Sub BuildUploadRequest(RequestBin)
    PosBeg = 1
    PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
    boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
    boundaryPos = InstrB(1,RequestBin,boundary)
        Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))
        Dim UploadControl
        Set UploadControl = CreateObject("Scripting.Dictionary")
        'Get an object name
        Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))
        Pos = InstrB(Pos,RequestBin,getByteString("name="))
        PosBeg = Pos+6
        PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
        Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
        PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))
        PosBound = InstrB(PosEnd,RequestBin,boundary)
            If  PosFile<>0 AND (PosFile<PosBound) Then
            PosBeg = PosFile + 10
            PosEnd =  InstrB(PosBeg,RequestBin,getByteString(chr(34)))
            FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))


            nomefile=filename
            UploadControl.Add "FileName", FileName
            Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
            PosBeg = Pos+14
            PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
            ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
            UploadControl.Add "ContentType",ContentType
            PosBeg = PosEnd+4
            PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
            Value = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
            Else
            Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
            PosBeg = Pos+4
            PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
            Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
        End If
        UploadControl.Add "Value" , Value   
        UploadRequest.Add name, UploadControl   
        BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
    Loop
End Sub
Function getByteString(StringStr)
 For i = 1 to Len(StringStr)
    char = Mid(StringStr,i,1)
    getByteString = getByteString & chrB(AscB(char))
 Next
End Function
Function getString(StringBin)
 getString =""
 For intCount = 1 to LenB(StringBin)
    getString = getString & chr(AscB(MidB(StringBin,intCount,1))) 
 Next
End Function

byteCount = Request.TotalBytes

RequestBin = Request.BinaryRead(byteCount)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")
BuildUploadRequest  RequestBin

然后从表单

请求.item的代码
blob = UploadRequest.Item("blob").Item("Value")

最后将代码保存在服务器中。我重命名名称的文件是因为没有重复名称,我创建了一个混合了日期和时间的univoque名称。

contentType = UploadRequest.Item("blob").Item("ContentType")
    filepathname = UploadRequest.Item("blob").Item("FileName")
    filename = Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))
    value = UploadRequest.Item("blob").Item("Value")
    Set ScriptObject = Server.CreateObject("Scripting.FileSystemObject")
    arrayFile = split(filename,".")
    estensioneFile = arrayFile(UBound(ArrayFile))
    namefileuploaded = day(date())& month(date()) & year(date())& hour(time())&minute(time())& second(time())&"a."&estensioneFile
    Set MyFile = ScriptObject.CreateTextFile(Server.mappath(folder)&"\"& namefileuploaded)
    For i = 1 to LenB(value)
        MyFile.Write chr(AscB(MidB(value,i,1)))
    Next
    MyFile.Close

答案 1 :(得分:1)

我看不到你的代码是如何工作的。

经典ASP无法访问像ASP.NET这样的上传文件(Request.UploadedFiles),所以如果你不使用一个COM组件,那么你需要读取Request.BinaryStream并解析出内容,这不是那么容易

有几个经典ASP脚本可以执行此操作,我建议您使用其中一个。我用过几次,没有任何问题。我建议你试试其中一个免费的:http://freevbcode.com/ShowCode.asp?ID=4596