简体   繁体   English

blueimp 的 jQuery-File-Upload - 附加标头

[英]jQuery-File-Upload by blueimp - additional headers

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?我已经通过wiki进行了搜索,但找不到答案我应该将附加标头(例如Authorization标头)放在 JS 脚本中的什么位置? Somewhere onSend / beforeSend or?某处onSend / beforeSend或?

Widget link: https://github.com/blueimp/jQuery-File-Upload插件链接: https://github.com/blueimp/jQuery-File-Upload

This is how I added the filename as a header:这就是我将文件名添加为 header 的方式:

$('#upload_btn').fileupload({
    singleFileUploads: true,
    beforeSend: function(xhr, data) {
        var file = data.files[0];
        xhr.setRequestHeader('X-FileName', file.name);
    },
});

Did you try to set additional headers through "options.headers" object?您是否尝试通过“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如果使用forceIframeTransport: true选项(IE 不支持 XHR 文件上传,您需要使用隐藏的 iframe 方法),那么修改标头不是一个选项: https://github.com/blueimp/jQuery-File-Upload /问题/654

Options.headers: http://api.jquery.com/jQuery.ajax/ 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.为文件上传插件设置的选项被传递给 jQuery.ajax() 并允许定义任何 ajax 设置或回调。

Try something like this..尝试这样的事情..

beforeSend: function(xhr) {
    xhr.setRequestHeader("Accept", "application/json");
    xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
}

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"},

Here is my implementation这是我的实现

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

  options.headers = {
    'Authorization': 'Bearer ' + accessToken
  };
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM