2012-09-20 125 views
0

我必须处理单个文件(文本)完美的处理程序。我可以收到.zip文件,但由于“损坏”错误而无法访问它们。我知道这是由于阅读东西的文本流而不是字节数组,但我无法弄清楚。 (我尝试低于)HTTP处理程序来处理.zips

编辑: 我需要能够有处理接受.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 

而不是Streamreader我使用BinaryReader。我试图内容保存为一个字节数组,然后使用WriteAllBytes方法他们全部写出来。

我会继续experiementing但任何指导,将是巨大的!

+0

的contentType都必须从文本。 – Aristos

+0

@Aristos我试图通过改变它八位字节。我想我的语法是,就在我将发布变更当我得到一个机会 – sealz

+0

这里后的实际代码,您尝试发送的压缩,而不是其他的这仍然是文本 – Aristos

回答

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