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:
The issue is your file writer, it is important to note that images are very forgiving when missing information.
I hope this FTP class will help you get on the right path: