michaelcbrook / simpleUpload.js

Unlike many JavaScript upload libraries on the interwebs, simpleUpload is an extremely simple yet powerful jQuery file upload plugin designed to be non-intrusive, backwards-compatible, flexible, and very easy to understand.
MIT License
65 stars 15 forks source link

Adding a CSRF token #1

Closed prattcmp closed 6 years ago

prattcmp commented 9 years ago

What should I do if I need to send a CSRF token with the file?

7ammer commented 8 years ago

+1

karneaud commented 7 years ago

+10000

leraei commented 6 years ago

I've added manually:

    function i(n) {
        if (1 == u(n)) {
            if (null != b) {
                if (void 0 == b[n] || null == b[n]) return void K(n, {name: "InternalError", message: "There was an error uploading the file"});
                if (window.FormData) {
                    var t = $.ajaxSettings.xhr();
                    if (t.upload) {
                        var a = b[n], o = new FormData;
                        f(o, F), o.append(C, a);
                        var token = $("meta[name='_csrf']").prop('content'), header = $("meta[name='_csrf_header']").prop('content');  // getting the tokenvalue and headername
                        var headers = {};
                        headers[header] = token;
                        var r = {
//putting it as headers:headers
                        url: e, data: o, headers: headers,type: "post", cache: !1, xhrFields: T, beforeSend: function (e) {
                                R[n].xhr = e
                            }, xhr: function () {
                                return t.upload.addEventListener("progress", function (e) {
                                    e.lengthComputable && Y(n, e.loaded / e.total * 100)
                                }, !1), t
                            }, error: function () {
                                R[n].xhr = null, K(n, {name: "RequestError", message: "Could not get response from server"})
                            }, success: function (e) {
                                R[n].xhr = null, Y(n, 100), G(n, e)
                            }, contentType: !1, processData: !1
                        };
                        return "auto" != E && (r.dataType = E), void $.ajax(r)
                    }
                }
            }
            "object" == typeof l && null !== l ? s(n) : K(n, {name: "UnsupportedError", message: "Your browser does not support this upload method"})
        }
    }
michaelcbrook commented 6 years ago

I know this is late, but you can now do this using the beforeSend callback: See here

It is nearly identical to jQuery's beforeSend in that it takes a jqXHR object and a settings object and you can modify the XHR using this function, before the request is made. While not specific to CSRF, this now allows you to add headers to your requests.