简体   繁体   中英

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 have 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 :)

I have done all this in a little bit cleaner way. You can find it here

cheers

Fahar

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