FTP上传破坏文件 [英] FTP upload corrupting files
问题描述
程序我正在处理Excel文件上传时损坏的文件。
本地计算机上的文件可以,但在远程计算机上,文件在Excel中打开时会损坏。 Excel能够修复文件,但是我不想避免这个问题。
到目前为止,我似乎没有任何图像文件问题。
公共功能UploadFile(BYVAL用户作为字符串,BYVAL OFILE作为FileInfo的)为布尔
尺寸ftpRequest作为的FtpWebRequest
尺寸ftpResponse作为FtpWebResponse
尝试
FtpCheckAndCreateDir(用户)
ftpRequest = CTYPE(FtpWebRequest.Create(基地+用户+ / + oFile.Name)的FtpWebRequest)
ftpRequest.Method = WebRequestMethods。 Ftp.UploadFile
ftpRequest.Proxy =什么
ftpRequest.UseBinary =真
ftpRequest.Credentials =中房 '新的NetworkCredential(...)
ftpRequest.KeepAlive =保持活动' 假
ftpRequest.EnableSsl = UseSSL'false
如果UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
Dim fileContents(oFile.Length)As Byte
使用fr作为FileStream = oFile.OpenRead
fr.Read(fileContents,0,Convert.ToInt32(oFile.Length))
End使用
使用写入器作为流= ftpRequest.GetRequestStream
writer.Write(fileContents,0,fileContents.Length)
结束使用
ftpResponse = CTYPE(ftpRequest.GetResponse,FtpWebResponse)
ftpResponse.Close()
ftpRequest = Nothing
返回True
Catch ex As WebException
返回False
结束尝试
End Function
编辑:
所以我拿了一个文件并通过coffeecup免费的ftp上传,下载了它,打开罚款。
我用我的程序来上传文件,然后用咖啡杯下载它,当我尝试在Excel中打开它时,它已经损坏。
我用HxD来比较这些文件,并且它回来后显示一条消息:所选的文件s是一样的。不过,文件大小是不同的!当我在两个文件上运行校验和时,它们会返回不同的值。
我不确定如何解决此问题或者我可以研究找到答案。
如果需要,我可以提供这些文件。 所以,经过大量的搜索和查找之后,我发现了这个答案: C# - 上传到服务器后文件已损坏
我可以使用它来获取文件上传而不被破坏。
使用旧零件的最终ftp函数注释掉,以便更容易看到更改:
公共功能UploadFile(BYVAL用户作为字符串,BYVAL OFILE作为FileInfo的)为布尔
尺寸ftpRequest作为的FtpWebRequest
尺寸ftpResponse作为FtpWebResponse
尝试
FtpCheckAndCreateDir(用户)
ftpRequest = CType(FtpWebRequest.Create(Base + User +/+ oFile.Name),FtpWebRequest)
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
ftpRequest.Proxy = Nothing
ftpRequest .UseBinary =真
ftpRequest.Credentials =中房
ftpRequest.KeepAlive =保持活动
ftpRequest.EnableSsl = UseSSL
,如果useSsl然后ServicePointManager.ServerCertificateValidationCallback =新RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
Dim fileContents(oFile.Length)As Byte
使用fr作为FileStream = oFile.OpenRead
使用writer作为Stream = ftpRequest.GetRequestStream
fr.CopyTo(writer)'这是重要的部分。
'writer.Write(fileContents,0,fileContents.Length)
结束使用
'fr.Read(fileContents,0,Convert.ToInt32(oFile.Length))
End使用
'使用writer作为Stream = ftpRequest.GetRequestStream
'writer.Write(fileContents,0,fileContents.Length)
'End使用
ftpResponse = CType(ftpRequest.GetResponse, FtpWebResponse)
ftpResponse.Close()
ftpRequest = Nothing
返回True
Catch ex As WebException
返回False
结束尝试
End Function
Program I am working on corrupts Excel files when it uploads them.
The file is ok on the local computer, but on the remote computer the file says corrupted when opening in Excel. Excel is able to repair the file, but I wan't to avoid the problem.
I don't seem to have any problems with image files so far.
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
EDIT:
So I took a file and uploaded it through coffeecup free ftp, downloaded it, and it opened fine.
I used my program to upload the file, and then downloaded it with with coffeecup and it came up as damaged when I tried to open it in excel.
I used HxD to compare the files, and it came back with a message saying: "The chosen files are identical. The files sizes are different, though!" and when I run a checksum on both files they come back with different values.
I'm not really sure how to troubleshoot this or what I can research to find the answer.
I can provide the files if needed.
So, after a lot of searching and looking, I found this answer: C# - File is corrupt after uploaded to server
which I was able to use to get a file to upload without being corrupted.
The final ftp function with old parts commented out to make it easier to see the changes:
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
ftpRequest.KeepAlive = KeepAlive
ftpRequest.EnableSsl = UseSSL
If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
Dim fileContents(oFile.Length) As Byte
Using fr As FileStream = oFile.OpenRead
Using writer As Stream = ftpRequest.GetRequestStream
fr.CopyTo(writer) ' This is the important part.
'writer.Write(fileContents, 0, fileContents.Length)
End Using
'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
这篇关于FTP上传破坏文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!