.. include:: ../Includes.txt .. _examples_legacy_fileuploads: ============ Uploading Files ============ .. warning:: | This example uses an JavaScript with a very "old" way of creating and sending XHR-requests. | You will only need this, if you are still forced to optimize for **Internet Explorer 11 and below**. For a more modern approach we would recommend using the promise-based :ref:`fetch() ` or a library that saves a lot of headaches like :ref:`Axios ` .. hint:: This chapter explains how to upload files to the ``nnrestapi`` using pure JavaScript without any libraries and shows a solution that is compatible with browser not supporting ES6+ and ``fetch()`` like Internet Explorer Version 11 and below. If you are interested in finding out, how to create end endpoint that processes the fileupload, attaches the SysFileReference to a model and then persists the model in the database, please refer to the examples in :ref:`this section ` How to upload files with pure JavaScript for older browsers ------------ .. tip:: You can find a full example with fileuploads and JavaScript here: `play on CodePen `__ Using mutipart form-data ~~~~~~~~~~~~ Please refer to :ref:`this section ` for detailled information on implementing the backend part of the file-upload. By default, the nnrestapi will recursively iterate through the JSON from your request and look for the special placeholder ``UPLOAD:/varname``. If it finds a fileupload in the multipart/form-data corresponding to the ``varname`` of the placeholder, it will automatically move the file to its destination and replace the ``UPLOAD:/varname`` in the JSON with the path to the file, e.g. ``fileadmin/api/image.jpg``. Here is a basic example on creating a multipart/form-data request using pure JavaScript. First, let's create a simple form in HTML. As we are retrieving the input-values manually, there is no need to wrap the inputs in a ``
`` element: .. code-block:: html