jQuery-File-Upload by blueimp - additional headers
Asked Answered
R

5

16

I've searched through wiki but couldn't find an answer where should I put my additional headers (for example Authorization header) in JS script? Somewhere onSend/beforeSend or?

Widget link: https://github.com/blueimp/jQuery-File-Upload

Red answered 4/4, 2012 at 13:12 Comment(0)
P
12

Did you try to set additional headers through "options.headers" object?

If using the forceIframeTransport: true option (with IE not supporting XHR file uploads you need to fall back on the hidden iframe approach), then modifying headers is not an option: https://github.com/blueimp/jQuery-File-Upload/issues/654

Options.headers: http://api.jquery.com/jQuery.ajax/

The options set for the File Upload plugin are passed to jQuery.ajax() and allow to define any ajax settings or callbacks.

Phalarope answered 5/4, 2012 at 16:55 Comment(2)
Where is this "headers" option you speak of? github.com/blueimp/jQuery-File-Upload/wiki/OptionsTunnell
@Mark: From that page: "The options set for the File Upload plugin are passed to jQuery.ajax()". So in addition to the listed options, you can also use all the options documented at api.jquery.com/jQuery.ajax. However, as the above answer explains, not all of them will necessarily work with the iframe transport.Tildatilde
T
29

This is how I added the filename as a header:

$('#upload_btn').fileupload({
    singleFileUploads: true,
    beforeSend: function(xhr, data) {
        var file = data.files[0];
        xhr.setRequestHeader('X-FileName', file.name);
    },
});
Tunnell answered 7/5, 2013 at 17:32 Comment(0)
P
12

Did you try to set additional headers through "options.headers" object?

If using the forceIframeTransport: true option (with IE not supporting XHR file uploads you need to fall back on the hidden iframe approach), then modifying headers is not an option: https://github.com/blueimp/jQuery-File-Upload/issues/654

Options.headers: http://api.jquery.com/jQuery.ajax/

The options set for the File Upload plugin are passed to jQuery.ajax() and allow to define any ajax settings or callbacks.

Phalarope answered 5/4, 2012 at 16:55 Comment(2)
Where is this "headers" option you speak of? github.com/blueimp/jQuery-File-Upload/wiki/OptionsTunnell
@Mark: From that page: "The options set for the File Upload plugin are passed to jQuery.ajax()". So in addition to the listed options, you can also use all the options documented at api.jquery.com/jQuery.ajax. However, as the above answer explains, not all of them will necessarily work with the iframe transport.Tildatilde
P
10

Try something like this..

beforeSend: function(xhr) {
    xhr.setRequestHeader("Accept", "application/json");
    xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
}
Phalarope answered 5/4, 2012 at 16:34 Comment(0)
C
2

The answer is quiet simple : just add your custom headers in add section

  add: function (e, data) {         

                data.headers={'X-Session-Id' : data.files[0].name.hashCode()};

                data.context = $('<button/>').text('Upload')
                .appendTo(document.body)

                .click(function () {
                    data.context = $('<p/>').text('Uploading...').replaceAll($(this));

                   // naam = naam.hashCode();
                    data.submit();
                });
        },

or in the initialisation :

$('#fileupload').fileupload({
        dataType: 'json',
        multipart : false,
        maxChunkSize: 10 * 1024 * 1024,
        headers:data.headers={'X-Session-Id' : "TEST-HEADER"},
Chere answered 12/10, 2013 at 13:37 Comment(2)
data is undefined in headers:data.headers={'X-Session-Id' : "TEST-HEADER"},Bagel
This is what worked for me: headers: { Authorization: 'Bearer ' + $rootScope.app.token }Gratianna
K
1

Here is my implementation

onSend: function(e, options) {
  var accessToken = ...;

  options.headers = {
    'Authorization': 'Bearer ' + accessToken
  };
},
Klagenfurt answered 5/3, 2013 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.