How to send a file (upload)?

To upload a file, use Eliom_parameter.file as service parameter type.

Ocsigen server will save the file at a temporary location and keep it there during the request. Then the file will be removed. You must link it somewhere else on the disk yourself if you want to keep it.

Be careful also to set the right option in Ocsigen server's configuration file. For example:

<ocsigen>
  <server>
    <uploaddir>/var/www/tmp</uploaddir>
    <maxuploadfilesize>2MB</maxuploadfilesize>
    <maxrequestbodysize>100MB</maxrequestbodysize>
  </server>
</ocsigen>

Example:

let upload = Eliom_service.Http.service
    ~path:["upload"]
    ~get_params:unit
    ()

let upload2 = 
  Eliom_registration.Html5.register_post_service
   ~fallback:upload
   ~post_params:(Eliom_parameter.file "file")
    (fun () file ->
      let newname = "/var/www/upload/thefile" in
      (try
        Unix.unlink newname;
      with _ -> ());
      Lwt_unix.link (Eliom_request_info.get_tmp_filename file) newname;
      Lwt.return
        (html
           (head (title (pcdata "Upload")) [])
           (body [h1 [pcdata "ok"]])))


let uploadform = 
  Eliom_registration.Html5.register upload
    (fun () () ->
      let f =
        (post_form upload2
           (fun file ->
             [p [file_input ~name:file ();
                 br ();
                 string_input ~input_type:`Submit ~value:"Send" ()
               ]]) ()) in
      Lwt.return
        (html
           (head (title (pcdata "form")) [])
           (body [f])))