我有一个处理单个文件(基于文本)的处理程序。我可以收到.zip文件但由于"腐败而无法访问它们#34;错误。我知道这是因为读取文本流中的内容而不是字节数组,但我无法弄明白。 (我的尝试在下面)
编辑: 我需要能够让处理程序接受.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
方法将它们全部写出来。
我将继续体验,但任何指导都会很棒!
答案 0 :(得分: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