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 回答