0

ファイルをバイナリ形式で API に投稿しています。

.pdf および .doc ファイルは問題ありません。これらは期待どおりにシステムに到着し、問題なく開くことができます。

しかし、何らかの理由で、.docx ファイルが破損していると表示されます。

なぜでしょうか?

Sub PostTheFile(CVFile, fullFilePath, PostToURL)

    strBoundary = "---------------------------9849436581144108930470211272"
    strRequestStart = "--" & strBoundary & vbCrlf &_
        "Content-Disposition: attachment; name=""file""; filename=""" & CVFile & """" & vbcrlf & vbcrlf
    strRequestEnd = vbCrLf & "--" & strBoundary & "--" 

    Set stream = Server.CreateObject("ADODB.Stream")
        stream.Type = adTypeBinary '1
        stream.Mode = adModeReadWrite '3    
        stream.Open
        stream.Write StringToBinary(strRequestStart)
        stream.Write ReadBinaryFile(fullFilePath)
        stream.Write StringToBinary(strRequestEnd)
        stream.Position = 0
        binaryPost = stream.read
        stream.Close

    Set stream = Nothing    

    Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
        httpRequest.Open "PATCH", PostToURL, False, "username", "pw"
        httpRequest.setRequestHeader "Content-Type", "multipart/form-data; boundary=""" & strBoundary & """"
        httpRequest.Send binPost
        Response.write "httpRequest.status: " & httpRequest.status 
    Set httpRequest = Nothing   
End Sub


Function StringToBinary(input)
    dim stream
    set stream = Server.CreateObject("ADODB.Stream")
        stream.Charset = "UTF-8"
        stream.Type = adTypeText 
        stream.Mode = adModeReadWrite 
        stream.Open
        stream.WriteText input
        stream.Position = 0
        stream.Type = adTypeBinary 
        StringToBinary = stream.Read
        stream.Close
    set stream = Nothing
End Function

Function ReadBinaryFile(fullFilePath) 
    dim stream
    set stream = Server.CreateObject("ADODB.Stream")
        stream.Type = 1
        stream.Open()
        stream.LoadFromFile(fullFilePath)
        ReadBinaryFile = stream.Read()
        stream.Close
    set stream = nothing
end function 

アップデート:

指摘されたように Stream.Close に追加されました。私はそれが問題を解決することを完全に期待していましたが、そうではありませんでした:(

更新 2:

さまざまなストリーム モードとエンコーディングをテストしてきましたが、何を試しても何の喜びもありません。

DOCX ドキュメントのデバッグも試みました。ドキュメント内のすべてのxmlファイルを調べて無効なxmlを探しました.

破損した docx ファイルをデバッグするにはどうすればよいですか?

4

2 に答える 2

1

docx ファイルのファイルタイプは「application/vnd.openxmlformats-officedocument.wordprocessingml.document」です。したがって、データソース テーブルのデータ型に nvarchar(max) を定義することで、この問題を解決できます。

于 2014-12-24T13:35:43.917 に答える
0

バイナリ ファイルの読み取り後にストリームを閉じませんでした

function ReadBinaryFile(fullFilePath) 
    dim stream
    set stream = Server.CreateObject("ADODB.Stream")
        stream.Type = 1
        stream.Open()
        stream.LoadFromFile(fullFilePath)
        ReadBinaryFile = stream.Read()
        stream.Close 'here
    set stream = nothing
end function 
于 2013-08-12T16:35:08.520 に答える