简体   繁体   English

用于处理.zips的HTTP处理程序

[英]HTTP Handler to Handle .zips

I have a handler that handles single files (text based) perfectly. 我有一个处理程序,可以完美地处理单个文件(基于文本)。 I can receive .zip files but they are unable to be accessed due to "corruption" errors. 我可以接收.zip文件,但是由于“损坏”错误而无法访问它们。 I know that this is due to reading things in as a text stream and not a byte array but I cannot figure it out. 我知道这是由于将内容作为文本流而不是字节数组读入,但是我无法弄清楚。 (My attempt is below) (我的尝试如下)

EDIT: I need to be able to have the handler accept .zips without corruption errors. 编辑:我需要能够使处理程序接受.zips而不会出现损坏错误。 I got past the corruption errors but the below code handles the file without corruption issues but unzips it with no files inside. 我克服了损坏错误,但是以下代码可以处理文件而不会出现损坏问题,但可以将其中的文件解压缩。

    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

Instead of Streamreader I am using BinaryReader . 我使用的是BinaryReader而不是Streamreader I have attempted to save contents as a byte array and then write them all out using the WriteAllBytes method. 我试图将内容保存为字节数组,然后使用WriteAllBytes方法将其全部WriteAllBytes

I will continue experiementing but any guidance would be great! 我将继续实验,但是任何指导都是很棒的!

I just solved the issue. 我刚刚解决了这个问题。 I simply needed to write it out to a byte array and save an integer to represent the number of bytes. 我只需要将其写到字节数组中并保存一个整数即可表示字节数。 Then simply print the contents. 然后只需打印内容。 It looks like I was trying to make things more complicated. 看来我正在尝试使事情变得更复杂。

My loop in original code is kinda ugly :( 我在原始代码中的循环有点难看:(

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM