0

単一のファイル(テキストベース)を完全に処理するハンドラーがあります。.zipファイルを受信できますが、「破損」エラーのためにアクセスできません。これは、バイト配列ではなくテキストストリームとして読み取るためであることがわかっていますが、理解できません。(私の試みは以下です)

編集:破損エラーなしでハンドラーに.zipを受け入れさせる必要があります。破損エラーを乗り越えましたが、以下のコードは破損の問題なしにファイルを処理しますが、ファイルが含まれていない状態で解凍します。

    Sub ProcessRequest(ByVal context as HttpContent) Implements IHTTPHandler.ProcessRequest

    Try
    If Context.Request.HttpMethod() = "POST" Then
    context.Response.ContentType = "application/octet-stream"
    context.Response.StatusCode = 204
    Dim reader as New System.IO.BinaryReader(context.Request.InputStream)
    Dim contents as Byte
    Dim int as Integer = reader.Basestream.Length

 ''Problem has got to be here, This loop structure can't be right..
    Do While int > 0
    contents = reader.readByte()
    System.IO.File.WriteAllText("thisismyoutputdirectory"), filename), contents)
    Loop
        else
    ''Handle non post cases
    end if

    Catch ex as Exception
    ''Error Handling is here
    End Try

    End Sub

代わりにStreamreaderを使用してBinaryReaderいます。WriteAllBytes内容をバイト配列として保存し、メソッドを使用してすべてを書き出そうとしました。

私は実験を続けますが、どんなガイダンスも素晴らしいでしょう!

4

1 に答える 1

0

問題を解決しました。それをバイト配列に書き出し、バイト数を表す整数を保存するだけで済みました。次に、内容を印刷するだけです。物事をより複雑にしようとしていたようです。

元のコードの私のループはちょっと醜いです:(

Sub ProcessRequest(ByVal context as HttpContext) Implements IHttpHandler.ProcessRequest

Try

   ''If the handler receives a POST requent then perform these actions    
    If context.Request.HttpMethod() = "POST" Then
      context.Response.ContentType = "application/octet-Stream"
      context.Response.StatusCode = 204
      ''Get the filename out of the requests header
      Dim filename as String = context.Request.Header("filename")
      ''Get the numbytes for the .zip and save them as a byte array
      Dim numbytes as Integer = reader.BaseStream.Length
      Dim contents() as Byte = reader.ReadBytes(numbytes)
     ''Write the byte array out to the file
     System.IO.File.WriteAllBytes("This/is/my/path/" & filename, contents)
    else
    '' Handle has no work to do since request was not a POST
    End if

    Catch
    ''Error Handling is here
    End Try
于 2012-09-21T11:58:43.733 に答える