Uploading from HTTP to the Arduino

I want my Arduino to serve a simple webpage, with only an upload form. Then, I want the file to be put on the SD card I have connected to my Arduino. Is such a thing possible? I know how to run a web server, and how to write the proper HTML, but I don't know how the Arduino should process it after.

I am working in this space at the moment with my arduino application at www.2wg.co.nz.

To upload files you need the file upload control on a web page and need to be able to process the html request uploaded to your arduino web server that includes the file content.

I have an operational version but it only loads about 1800 bytes and stops. I have not progressed the subject for a couple of weeks - other things to do.

To solve the problem you need to use string (or String) parsing of the html request.

There does not appear to be a ready built library available.

Cheers

CatweazleNZ

After some more work I now have what seems to be a robust operational implementation.

I can load any file from a browser through my Arduino application and onto the Arduino's micro SD card in a directory/folder of my choice.

The following is an example html POST request for a file upload:

POST /7166/ HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://www.2wg.co.nz/20611/
Accept-Language: en-NZ,th;q=0.5
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: multipart/form-data; boundary=---------------------------7deff1f203a6
Accept-Encoding: gzip, deflate
Host: www.2wg.co.nz
Content-Length: 309
DNT: 1
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: mstnc=3; mstmode=manual; SID=227568

-----------------------------7deff1f203a6
Content-Disposition: form-data; name="folder"

/UPLOAD/
-----------------------------7deff1f203a6
Content-Disposition: form-data; name="filename"; filename="QBF.txt"
Content-Type: text/plain

The quick brown fox.
-----------------------------7deff1f203a6--

The above request is uploading file QBF.txt to the /UPLOAD/ folder of my Arduino system's micro SD card. The content of the file is "The quick brown fox.".

To upload this file you need to:

  1. Parse the initial html request data to extract various data including the multi-part form boundary.
  2. Parse the multi-part form section of the html request (as delimited by the boundaries) to extract the filename, destination folder and the file content.
  3. Write the file content to your micro SD card in the designated file and folder.

To be sure of accurate processing you should read the Content-Length: value and then count off the data length as you process it. Your operation will be OK once you have processed the ending boundary and cross checked the overall length of the data that you have processed is equal to the Content-Length value.

The above operations work the same for binary files as well as text files - although binary file content cannot be displayed as an example as above.

File upload is not quick. It takes say 75 seconds to upload a 130KB jpeg file - but my Arduino can stream that file straight back to my browser as a file display in five seconds. In my search for extra speed I have optimised the QOS settings in my Netgear ADSL modem/router for some improvement. I am continuing to investigate timings for SD card file write operations (I use a 64 byte buffer), ethernet data buffering, ethernet data reading, SPI switching, network delays and the logic of my sketch code, etc, etc, to see if I can make it go faster.

I will continue to test and refine this functionality for a couple of weeks. I may release the code via a small web server application featuring micro SD card browsing, file display and file upload.

Cheers

Catweazle NZ.

Hi guys,

I am very interested in a code example on Arduino-side for file-uploading. Can anyone post some?

@Catweazle, I was checking your website, found it interesting. Could you please keep us informed about code examples?

Thanx in advance.