How to configure ocsigen to upload a file?

Hi !
Based on ocsigen documentation http://ocsigen.org/tuto/6.4/manual/how-to-send-file-upload and http://ocsigen.org/ocsigenserver/2.9/manual/config.html#upload , I tried to upload a document to an ocsigen app (I tested it on my machine, Ubuntu 18). I used code from tutorial that I modified to include path to some directory on my machine, and I added the path to that directory in .conf.in file inside ocsigen and server xml nodes as needed. I could run the server, but when I submit a file on localhost through my browser, I get a “forbidden error 403”. I think I am doing something wrong. Maybe I mess up between absolute path on my machine, virtual absolute path on the app, and relative path… As I found documentation not very helpful on this, I tried several things unsuccessfully.
Here is the code of what I tried :

[%%shared
    open Eliom_lib
    open Eliom_content
    open Html.D
]

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

let upload2 =
  Eliom_registration.Html.create (*I corrected here the tutorial: Http <- Html*)
    ~path:(Eliom_service.Path ["upload"])
    ~meth:(Eliom_service.Post
             (Eliom_parameter.unit,
              Eliom_parameter.file "file"))
    (fun () file ->
      let newname = "/home/milan/prog/ocaml/2019/ocsigen-learn/upload_doc/local/var/www/tmp/thefile" in
      (try
        Unix.unlink newname;
      with _ -> ());
      Lwt_unix.link (Eliom_request_info.get_tmp_filename file) newname;
      Lwt.return
        (html
           (head (title (txt "Upload")) [])
           (body [h1 [txt "ok"]])))

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

and in the upload-doc.conf.in :

<ocsigen>
  <server>
    <port>%%PORT%%</port>
    [...]
    <host hostfilter="*">
      <static dir="%%STATICDIR%%" />
      <static dir="%%ELIOMSTATICDIR%%" />
      <eliommodule module="%%LIBDIR%%/%%PROJECT_NAME%%.cma" />
      <eliom/>
    </host>
    <uploaddir>/home/milan/prog/ocaml/2019/ocsigen-learn/upload_doc/local/var/www/tmp</uploaddir>
    <maxuploadfilesize>2MB</maxuploadfilesize>
    <maxrequestbodysize>100MB</maxrequestbodysize>
  </server>
</ocsigen>

I tried several things like the /var/www/tmp exemple (creating manually a tmp directory in local/var/www), putting it inside the static directory or setting a UPLOADDIR variable inside Makefile.options.
Could someone give me an insigth on what I have to do very precisely ?
Best !

Does it work when you put <uploaddir>, <maxuploadfilesize> and <maxrequestbodysize> before <host> in config file?
Ocsigen server’s configuration file is order dependent.

1 Like

It was indeed the problem, and it worked fine with <uploaddir> , <maxuploadfilesize> and <maxrequestbodysize> before <host> in config file.
Thanks a lot and happy new year !

1 Like