简体   繁体   中英

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. 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. 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 . I have attempted to save contents as a byte array and then write them all out using the WriteAllBytes method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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