Download file via HTTP-request/response

I'm working with a Arduino Mega + a Wifishield v2.2 from Dfrobot.

I have the communicaion up&working.

Can send a HTTP-request from the broweser, and answer back with a HTTP-response + HTML data. So far so good.

Now i want to be able to download a small file, a csv file in my case.

Does anyone have a good code for this? I'we seen alot of examples, but none seems to work.

Note that the shield is lacking a webserver-functionality, so the HTTP-header must be created in real time by the Arduino.

Here's an example (that dosen't work):

Serial.println("\n\nSending HTTP RESPONSE WITH CSV-file");
  Serial.println("HTTP/1.1 200 OK\r\n");
  Serial.println("Content-Type: application/CSV"); //text/plain
  Serial.println("content-disposition: attachment; filename=\"myfileName.csv\"");
  //	Serial.print("Content-Type: text/csv");
  //	Serial.print("\r\n");
  Serial.println("Content-Length: 1000");
  //	Serial.println("Connection: close");
  //	Serial.println("Refresh: 5");  // refresh the page automatically every 5 sec
  //	Serial.print("\r");
  //	Serial.print("\n");
  //     Serial.println("Content-Type: text/csv");
  //         Serial.println();

I'll leave my out-commented lines (residue from a lot of experimenting) in there.

  Serial.println("Content-Length: 1000");

How do you know that there are 1000 characters in the file?

Maybe the fine folks at http://snippets-r-us.com can help you with your snippets.

Where is he cvs file stored?

To answer both your questions.
The csv is created in real-time.
I know the exact size before-hand, since i know how many values and the length of every value.

The 1000 here above was just an example.

I'll check with the fine folks at snippets-r-us. Thanx for the tip.

I know the exact size before-hand, since i know how many values and the length of every value.

The 1000 here above was just an example.

Using the actual value in the example would have made more sense. You said that that collection of statements was stuff that didn't work, without defining what they actually accomplished, and how that differed from what you expected. So, we can only surmise that your expectations are wrong, or that the order of the statements actually used was incorrect.

I'll check with the fine folks at snippets-r-us. Thanx for the tip.

See you soon.

This won't work. It sends a blank line, indicating end of header.

  Serial.println("HTTP/1.1 200 OK\r\n");

Remove the "\r\n" from it.

I did not check the rest of it because you didn't include it in your post. That was what PaulS was saying.
"If the OP thinks it isn't important enough to post, it probably is".

Here's an example (that dosen't work):

The Zen-inclined among us may wonder if an example that doesn't work is actually an example?

Actually, the code i posted was all i had at the time. The result was that nothing happened.
My expectations were a downloaded CSV-file. Obviously.

So i gave all the info i had at the time. I also decided to include the code i had at that time, in order to show what i've tried.
If i hadn't post a example code, you guys would've moaned about the absence of an example, etc etc.

SurferTim was right though, double break lines doesn't seem to work. However, this was not the only problem.

Now, i got it to work, almost.

an update:
After some testing, i almost got this working:

an update:
After some testing, i almost got this working:

HTTP/1.1 200 OK
Content-Description: File Transfer"
Content-Type: application/force-download
Content-Type: application/CSV
content-disposition attachment;filename="myfile.csv"
Pragma: public
Pragma: no-cache
Content-Length: 20
 \r\n 
1;2;3;4;5;6;7:8;9;0;\r\n"

The browser starts to download a file, however, the filename is not "myfile.csv". The filename comes from my "querystring".
For example: http://192.168.111.1/xyz
HTTP-request : GET /xyz/HTTP/1.1

the downloaded file is called xyz (without extension).

Any ideas why the "content-disposition attachment;filename="myfile.csv" is not working?

Headers must have a colon separating them from the rest of the line.

Try this:

Content-disposition: attachment;filename="myfile.csv"

-br

billroy:
Headers must have a colon separating them from the rest of the line.

Try this:

Content-disposition: attachment;filename="myfile.csv"

-br

haha... of course, how did i miss that one!? :S
Thanx alot billroy!

SurferTim:
This won't work. It sends a blank line, indicating end of header.

  Serial.println("HTTP/1.1 200 OK\r\n");

Remove the "\r\n" from it.

I did not check the rest of it because you didn't include it in your post. That was what PaulS was saying.
"If the OP thinks it isn't important enough to post, it probably is".

This was not the problem.
I have the double enofLines in my code now, and it works just fine.
After the content-length however, there must be empty row.

hbx2013:

SurferTim:
This won't work. It sends a blank line, indicating end of header.

  Serial.println("HTTP/1.1 200 OK\r\n");

Remove the "\r\n" from it.

I did not check the rest of it because you didn't include it in your post. That was what PaulS was saying.
"If the OP thinks it isn't important enough to post, it probably is".

This was not the problem.
I have the double enofLines in my code now, and it works just fine.
After the content-length however, there must be empty row.

Yes it was, just not the only one.

SurferTim:

hbx2013:

SurferTim:
This won't work. It sends a blank line, indicating end of header.

  Serial.println("HTTP/1.1 200 OK\r\n");

Remove the "\r\n" from it.

I did not check the rest of it because you didn't include it in your post. That was what PaulS was saying.
"If the OP thinks it isn't important enough to post, it probably is".

This was not the problem.
I have the double enofLines in my code now, and it works just fine.
After the content-length however, there must be empty row.

Yes it was, just not the only one.

Well, strange, considering it works for me right now, with that line intact...

So... to take this a step further.

Anyone know if it's possible to use a "start-of-data"-> data -> "end-of-data"?

In my case, i knew the exact size, hence: Content-Length: xxxxx

I have a new case where i don't know the size of the file.

Any ideas?

I have a new case where i don't know the size of the file.

How can you not know the size of the file?

Try leaving out the Content-length: header line entirely. It is optional in the HTTP spec.

The bitlash web server doesn't send content-length: bitlash/BitlashWebServer.ino at master · billroy/bitlash · GitHub

Be sure to close the client connection after writing your last byte, since this is the only way the other end will have to know the response body is finished.

-br