File upload without server script?

File upload without server script?

krupal.jadhavkrupal.jadhav Posts: 14Questions: 6Answers: 0

I am making an application using plain JAVASCRIPT, HTML and REST API (Mulesoft). Is there a way I can implement the file upload function without writing any server side script and simply sending the file to the REST API?

Answers

  • allanallan Posts: 63,331Questions: 1Answers: 10,436 Site admin

    What does your REST API expect for the file upload in terms of parameters and data?

    Allan

  • krupal.jadhavkrupal.jadhav Posts: 14Questions: 6Answers: 0

    @allan it expects a multipart form-data

  • krupal.jadhavkrupal.jadhav Posts: 14Questions: 6Answers: 0

    I wrote my own code for uploading files without server script. I created a datatables button and wrote the file handling code in action, here it is:

    {
        text: 'Upload CSV',
        action: function (e, dt, node, config) {
            $('#trigger').click();
            $('#uploadfile').on('click', function () {
                $.ajax({
                    url: fileurl,
                    type: 'POST',
                    data: new FormData($('form')[0]),
                    headers: {
                        'Authorization': 'Bearer ' + session_token
                    },
                    cache: false,
                    contentType: false,
                    processData: false,
                    xhr: function () {
                        var myXhr = $.ajaxSettings.xhr();
                        if (myXhr.upload) {
                            myXhr.upload.addEventListener('progress', function (e) {
                                if (e.lengthComputable) {
                                    $('progress').attr({
                                        value: e.loaded,
                                        max: e.total,
                                    });
                                }
                            }, false);
                        }
                        return myXhr;
                    }
                });
                table.ajax.load();
    
            });
        }
    }
    
  • allanallan Posts: 63,331Questions: 1Answers: 10,436 Site admin

    Thanks for sharing that with us. Great to hear you got it working.

    Allan

This discussion has been closed.