why are docx files corrupted by binary post, but .doc and .pdf are fine?

邮差的信 提交于 2020-01-03 05:19:21

问题


I'm posting files to an API in binary format.

.pdf and .doc files are fine - they arrive in the system as expected and open up without any problems.

But for some reason, .docx files show up as corrupt.

Why would that be?

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 

Update:

Added in Stream.Close as pointed out. I fully expected that to solve the problem but it didn't :(

Update 2:

I've been testing with different stream modes and encodings, but nothing I try gives me any joy.

I've also tried debugging the DOCX document. I've been through all the xml files within the document looking for invalid xml - I thought this might give me a clue as to where it's going wrong, but it all comes out as valid.

How can I debug a corrupt docx file?


回答1:


The file type of docx file is "application/vnd.openxmlformats-officedocument.wordprocessingml.document". So you can solve this problem by defining nvarchar(max) for data type in your datasource table.




回答2:


You did not close the stream after reading the binary file

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 


来源:https://stackoverflow.com/questions/18191987/why-are-docx-files-corrupted-by-binary-post-but-doc-and-pdf-are-fine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!