FTP上传损坏的文件

时间:2014-08-06 15:33:03

标签: vb.net ftp

程序我正在处理上传它们时损坏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上传,下载了,打开就好了 我使用我的程序上传文件,然后用咖啡杯下载它,当我尝试在excel中打开它时它出现了损坏。
我使用HxD来比较文件,然后它返回一条消息说:"所选文件是相同的。但是文件大小不同!"当我对两个文件运行校验和时,它们会返回不同的值。

我不确定如何解决这个或我可以研究什么来找到答案 如果需要,我可以提供文件。

2 个答案:

答案 0 :(得分:0)

所以,经过大量的搜索和寻找,我找到了这个答案:C# - File is corrupt after uploaded to server

我可以使用它来上传文件而不会被破坏。

带有旧部件的最终ftp函数已注释掉,以便更容易查看更改:

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

答案 1 :(得分:0)

问题在于您的文件编写者,重要的是要注意图像在丢失信息时非常宽容。

我希望这个FTP课程能帮助你走上正确的道路:

Public Class FTP
    '-------------------------[BroCode]--------------------------
    '----------------------------FTP-----------------------------
    Private _credentials As System.Net.NetworkCredential
    Sub New(ByVal _FTPUser As String, ByVal _FTPPass As String)
        setCredentials(_FTPUser, _FTPPass)
    End Sub
    Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String)
        Dim _FileInfo As New System.IO.FileInfo(_FileName)
        Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
        _FtpWebRequest.Credentials = _credentials
        _FtpWebRequest.KeepAlive = False
        _FtpWebRequest.Timeout = 20000
        _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
        _FtpWebRequest.UseBinary = True
        _FtpWebRequest.ContentLength = _FileInfo.Length
        Dim buffLength As Integer = 2048
        Dim buff(buffLength - 1) As Byte
        Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()
        Try
            Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()
            Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)
            Do While contentLen <> 0
                _Stream.Write(buff, 0, contentLen)
                contentLen = _FileStream.Read(buff, 0, buffLength)
            Loop
            _Stream.Close()
            _Stream.Dispose()
            _FileStream.Close()
            _FileStream.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Upload Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Public Sub DownloadFile(ByVal _FileName As String, ByVal _ftpDownloadPath As String)
        Try
            Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpDownloadPath)
            _request.KeepAlive = False
            _request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
            _request.Credentials = _credentials
            Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
            Dim responseStream As System.IO.Stream = _response.GetResponseStream()
            Dim fs As New System.IO.FileStream(_FileName, System.IO.FileMode.Create)
            responseStream.CopyTo(fs)
            responseStream.Close()
            _response.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Download Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Public Function GetDirectory(ByVal _ftpPath As String) As List(Of String)
        Dim ret As New List(Of String)
        Try
            Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpPath)
            _request.KeepAlive = False
            _request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
            _request.Credentials = _credentials
            Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
            Dim responseStream As System.IO.Stream = _response.GetResponseStream()
            Dim _reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream)
            Dim FileData As String = _reader.ReadToEnd
            Dim Lines() As String = FileData.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
            For Each l As String In Lines
                ret.Add(l)
            Next
            _reader.Close()
            _response.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Directory Fetch Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return ret
    End Function

    Private Sub setCredentials(ByVal _FTPUser As String, ByVal _FTPPass As String)
        _credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
    End Sub
End Class