jQuery-File-Upload by blueimp - additional headers

前端 未结 5 1790
野的像风
野的像风 2021-02-05 11:19

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 o

相关标签:
5条回答
  • 2021-02-05 11:52

    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"},
    
    0 讨论(0)
  • 2021-02-05 11:53

    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.

    0 讨论(0)
  • 2021-02-05 11:56

    Try something like this..

    beforeSend: function(xhr) {
        xhr.setRequestHeader("Accept", "application/json");
        xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
    }
    
    0 讨论(0)
  • 2021-02-05 12:05

    Here is my implementation

    onSend: function(e, options) {
      var accessToken = ...;
    
      options.headers = {
        'Authorization': 'Bearer ' + accessToken
      };
    },
    
    0 讨论(0)
  • 2021-02-05 12:11

    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);
        },
    });
    
    0 讨论(0)
提交回复
热议问题