为什么docx文件由二进制后损坏,但.doc和.PDF都很好? [英] why are docx files corrupted by binary post, but .doc and .pdf are fine?
问题描述
我张贴的文件以二进制格式的API。
I'm posting files to an API in binary format.
.pdf和.doc文件都很好 - 他们在系统到达预期,开辟没有任何问题。
.pdf and .doc files are fine - they arrive in the system as expected and open up without any problems.
但由于某些原因,.DOCX文件显示为损坏。
But for some reason, .docx files show up as corrupt.
为什么会是这样?
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添加为指出。我完全可以预料,要解决这个问题,但事实并非如此:(
Added in Stream.Close as pointed out. I fully expected that to solve the problem but it didn't :(
更新2:
我一直在不同的流模式和编码测试,但没有我尝试让我的喜悦。
I've been testing with different stream modes and encodings, but nothing I try gives me any joy.
我也试过调试DOCX文档。我经历过找无效的XML文档中的所有XML文件 - 我想这可能给我一个线索,它是怎么回事错的,但是这一切都出来为有效。
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.
推荐答案
的docx文件的文件类型是应用程序/ vnd.openxmlformats-officedocument.wordprocessingml.document。
所以,你可以在你的数据源表中定义的数据类型为nvarchar(最大)解决了这个问题。
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.
这篇关于为什么docx文件由二进制后损坏,但.doc和.PDF都很好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!