使用敏捷包保存后上传文档失败

时间:2012-11-02 13:13:13

标签: asp.net html-agility-pack ftpwebrequest

在使用Html Agility Pack保存文件后,我一直在努力解决文件上传(通过FtpWebRequest)的问题。 我正在尝试编辑html文件,然后将其保存到stringWriter,然后将其上传到服务器。 我正在保存这样的文件     ...

doc.OptionAutoCloseOnEnd = True
doc.OptionWriteEmptyNodes = True
Dim sr As New StringWriter
doc.Save(sr)

然后使用asyncFtpUpload sub上传它,就像msdn上的一样,但只有一些更改(而不是我使用字符串的文件名)

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx#Y3024

结果是上传的文件得到了最后一个字节被截止。 当我在服务器上看到该文件的源代码时,它错过了< \ html>标签。 我已经调试了代码,并且在doc.save()上创建的字符串是正确的,然后在上传例程中,当我使用getBytes()它仍然正确时,requestStream write方法写入正确的流长度。 我无法弄清楚这段代码发生了什么。

任何人都可以帮我解决这个问题吗?

以下是代码:

Dim outStream As MemoryStream = New MemoryStream(ASCIIEncoding.Default.GetBytes(str))
Dim uploader As AsynchronousFtpUpLoader = New AsynchronousFtpUpLoader
uploader.startUpload(pag, outStream)

上课:

Public Class AsynchronousFtpUpLoader

Public Sub startUpload(ByVal pag As FtpPage, ByVal stream As Stream)
    Try
        Dim waitObject As ManualResetEvent

        Dim target As New Uri(pag.currentUrl)
        Dim state As New FtpState()
        Dim request As FtpWebRequest = DirectCast(WebRequest.Create(target), FtpWebRequest)
        request.Method = WebRequestMethods.Ftp.UploadFile
        request.UseBinary = False
        request.Credentials = New NetworkCredential(pag.login, pag.password)

        state.Request = request
        state.stream = stream
        ' Get the event to wait on.
        waitObject = state.OperationComplete

        ' Asynchronously get the stream for the file contents.
        request.BeginGetRequestStream(New AsyncCallback(AddressOf EndGetStreamCallback), state)

        ' Block the current thread until all operations are complete.
        waitObject.WaitOne()

        ' The operations either completed or threw an exception. 
        If state.OperationException IsNot Nothing Then
            Throw state.OperationException
        Else

        End If
    Catch ex As Exception

    End Try

End Sub

Private Shared Sub EndGetStreamCallback(ByVal ar As IAsyncResult)
    Dim state As FtpState = DirectCast(ar.AsyncState, FtpState)

    Dim requestStream As Stream = Nothing

    Try
        requestStream = state.Request.EndGetRequestStream(ar)

        Const bufferLength As Integer = 2048
        Dim buffer As Byte() = New Byte(bufferLength) {}
        Dim readBytes As Integer = 0
        Dim stream As MemoryStream = state.stream
        Do
            readBytes = stream.Read(buffer, 0, bufferLength)
            If readBytes <> 0 Then
                requestStream.Write(buffer, 0, readBytes)
            End If
        Loop While readBytes <> 0

        requestStream.Flush()
        state.stream.Close()
        requestStream.Close()
        state.Request.BeginGetResponse(New AsyncCallback(AddressOf EndGetResponseCallback), state)
    Catch e As Exception
        state.OperationException = e
        state.OperationComplete.[Set]()
        Return
    End Try

End Sub

Private Shared Sub EndGetResponseCallback(ByVal ar As IAsyncResult)
    Dim state As FtpState = DirectCast(ar.AsyncState, FtpState)
    Dim response As FtpWebResponse = Nothing
    Try
        response = DirectCast(state.Request.EndGetResponse(ar), FtpWebResponse)
        response.Close()
        state.StatusDescription = response.StatusDescription
        state.OperationComplete.[Set]()
    Catch e As Exception
        state.OperationException = e
        state.OperationComplete.[Set]()
    End Try
End Sub

0 个答案:

没有答案