Warning: Reason support is experimental. We are looking for beta-tester and contributors.

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:


Example with Eliom_content.Html.D module opened:

let upload = Eliom_service.create
    ~path:(Eliom_service.Path ["upload"])
    ~meth:(Eliom_service.Get Eliom_parameter.unit)

let upload2 =
    ~path:(Eliom_service.Path ["upload"])
              Eliom_parameter.file "file"))
    (fun () file ->
      let newname = "/var/www/upload/thefile" in
        Unix.unlink newname;
      with _ -> ());
      let%lwt () =
        Lwt_unix.link (Eliom_request_info.get_tmp_filename file) newname
           (head (title (txt "Upload")) [])
           (body [h1 [txt "ok"]])))

let uploadform =
  Eliom_registration.Html.register ~service:upload
    (fun () () ->
      let f =
        (Form.post_form ~service:upload2
           (fun file ->
             [p [Form.file_input ~name:file ();
                 br ();
                 Form.input ~input_type:`Submit ~value:"Send" Form.string
               ]]) ()) in
           (head (title (txt "form")) [])
           (body [f])))