【发布时间】:2014-08-06 15:33:02
【问题描述】:
我正在处理的程序在上传 Excel 文件时会损坏它们。
该文件在本地计算机上正常,但在远程计算机上,该文件在 Excel 中打开时显示已损坏。 Excel 可以修复文件,但我不想避免这个问题。
到目前为止,我似乎对图像文件没有任何问题。
Public Function UploadFile(ByVal User As String, ByVal oFile As FileInfo) As Boolean
Dim ftpRequest As FtpWebRequest
Dim ftpResponse As FtpWebResponse
Try
FtpCheckAndCreateDir(User)
ftpRequest = CType(FtpWebRequest.Create(Base + User + "/" + oFile.Name), FtpWebRequest)
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
ftpRequest.Proxy = Nothing
ftpRequest.UseBinary = True
ftpRequest.Credentials = Cred ' New NetworkCredential(...)
ftpRequest.KeepAlive = KeepAlive ' false
ftpRequest.EnableSsl = UseSSL ' false
If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
Dim fileContents(oFile.Length) As Byte
Using fr As FileStream = oFile.OpenRead
fr.Read(fileContents, 0, Convert.ToInt32(oFile.Length))
End Using
Using writer As Stream = ftpRequest.GetRequestStream
writer.Write(fileContents, 0, fileContents.Length)
End Using
ftpResponse = CType(ftpRequest.GetResponse, FtpWebResponse)
ftpResponse.Close()
ftpRequest = Nothing
Return True
Catch ex As WebException
Return False
End Try
End Function
编辑:
于是我拿了一个文件,通过coffeecup free ftp上传,下载了,打开就好了。
我用我的程序上传文件,然后用coffeecup下载它,当我试图在excel中打开它时它出现了损坏。
我使用 HxD 比较文件,它返回一条消息说:“选择的文件是相同的。但是文件大小不同!”当我对两个文件运行校验和时,它们会返回不同的值。
我不确定如何解决此问题或我可以研究什么来找到答案。
如果需要,我可以提供文件。
【问题讨论】: