开发者

ASP.Net not populating Request.Files on receiving multipart data

I'm sending files from an android app to a asp.net webform using multipart/form-data as the content type. However the Request.files property does not get populated. Reading the Request object I get the following

Request.Params("ALL_HTTP")

"HTTP_CONNECTION:Keep-Alive HTTP_CONTENT_LENGTH:8913 HTTP_CONTENT_TYPE:multipart/form-data;boundary=*********************** HTTP_HOST:192.168.1.2 HTTP_USER_AGENT:Dalvik/1.2.0 (Linux; U; Android 2.2; sdk Build/FRF91) "

The HTTP_CONTENT_LENGTH shows the correct length. I guess I will hav开发者_如何学JAVAe to do a binary read and then parse the content and store the file contents. Has anyone done this before or is there a library/class available?

Thanks


How are you writing the files to the request stream? The following rules should be followed when programatically uploading files (binary streams):

1) Write a boundary (it could be anything prefixed by two dashes). Here is an example boundary:

private string boundary = "----" + DateTime.Now.Ticks;

2) Write content disposition in the form:

Content-Disposition: form-data; name="{name}"; filename="{filename}" 

3) Write the content type

4) Write an empty line

5) Write the bytes to the request stream

6) Write the end boundary, it marks the end of the request. It should be in the following form:

"--" + boundary + "--"

7) Write an empty line and flush (if needed) the request.

Here is how sample upload request should look inside an Http debugging tool like fiddler:

------634388181001966332

Content-Disposition: form-data; name="files"; filename="cald_3d.JPG"

Content-Type: application/octet-stream

1010101001... (more bytes)

------634388181001966332--

Then, on the server, access the file with Request.Files[name], the same name which you have used when specifying Content Disposition. Good luck :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜