【问题标题】:HTTP Handler to Handle .zips处理 .zip 的 HTTP 处理程序
【发布时间】:2012-09-20 19:37:35
【问题描述】:

我有一个处理程序可以完美地处理单个文件(基于文本)。我可以接收 .zip 文件,但由于“损坏”错误而无法访问它们。我知道这是由于以文本流而不是字节数组的形式读取内容,但我无法弄清楚。 (下面是我的尝试)

编辑: 我需要能够让处理程序接受 .zips 而不会出现损坏错误。我克服了损坏错误,但下面的代码处理文件时没有损坏问题,但解压后没有文件。

    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

我使用的是BinaryReader,而不是Streamreader。我尝试将内容保存为字节数组,然后使用WriteAllBytes 方法将它们全部写出。

我会继续尝试,但任何指导都会很棒!

【问题讨论】:

  • 内容类型必须从文本更改。
  • @Aristos 我尝试将其更改为八位字节。我认为我的语法刚刚好,我会在有机会时发布。
  • 在此处发布您尝试以 zip 格式发送的实际代码,而不是其他仍为文本的代码。
  • 我很困惑 - 您阅读了 Request.InputStream 并尝试将其作为 zip 发回。你有你赢得的文件要在文件中发送还是你试图发送的这是什么Request.InputStream
  • @Aristos 感谢您的帮助。我发现了这个问题,我的答案如下。随意总结我的答案并添加您可能拥有的任何其他信息。我想奖励你一些代表。

标签: asp.net vb.net http bytearray handler


【解决方案1】:

我刚刚解决了这个问题。我只需要将其写入字节数组并保存一个整数来表示字节数。然后简单地打印内容。看起来我试图让事情变得更复杂。

我在原始代码中的循环有点丑:(

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    相关资源
    最近更新 更多