Products > Programming

What is the HTTP server-client interaction to do file transfers?

(1/14) > >>

Back here
I implemented a favicon on my simple server.

I now understand how a web page is served and how one can have links on that page and deliver other pages as somebody clicks on the links. It seems really simple.

However I now need to do file transfers, of arbitrary size. My understanding that the client end is a standard browser feature. On a download, the browser offers a Save As dialog. On an upload, it offers a file browser to pick the file. It is the transfer I don't know. I think it is done in blocks, chosen to suit the server memory availability, and the client has to request the next block. All examples I see online again apply to standard servers like Apache. Can anyone tell me the actual data flowing?

Many thanks :)

File downloads are handled like any other request.  The client decides whether to try to display it or download it by default based on file type.

File uploads are done with form submission.  A file upload must use the submission method "POST" not "GET". There is no chunking, and it isn't normally broken into multiple requests.  Throttling is just done by standard TCP connection throttling.

There is a "content range" field to request certain byte ranges that is used to get early previews in some document types but it's up to the client and often not used.

Nominal Animal:
Downloads are served exactly like HTML pages, only the Content-Type: defines the file type.
If you want to force the file to be saved (and not opened in an application), use Content-Type: application/octet-stream.

File uploads use POST method requests, where the header part is followed (by an empty line and) the uploaded file data.
The Content-Type is multipart/form-data; boundary=boundarystring with the boundarystring usually quoted.
Mozilla Developer Network has an example:

--- Code: ---POST /test HTTP/1.1
Host: foo.example
Content-Type: multipart/form-data;boundary="boundary"

Content-Disposition: form-data; name="field1"

Content-Disposition: form-data; name="field2"; filename="example.txt"


--- End code ---
Note that each newline in the above snippet is \r\n.  Browsers tend to use long boundary strings, which can be annoying.

I think there's a specific request for partial transfers too, but I don't remember the details.

Nominal Animal:
Unless the server provides an Accept-Ranges HTTP header, clients shouldn't try to request ranges or partial responses.  Requests themselves are always complete, not split into ranges.

If a request does contain a Range HTTP header, it is up to the server to completely ignore it (just return a normal 200 Ok response with the entire contents), return only the range(s) (in a 206 Partial Content response), or error out with 416 Range Not Satisfiable.  The most robust approach is to ignore it, and just always return full contents.


[0] Message Index

[#] Next page

There was an error while thanking
Go to full version