简体   繁体   English

jQuery BlueImp文件上传授权

[英]jQuery BlueImp File Upload Authorization

I am trying to implement the jQuery Bluimp File Upload control (from here: https://github.com/blueimp/jQuery-File-Upload ) with an ASP.NET WebAPI backend, but having a little trouble. 我正在尝试使用ASP.NET WebAPI后端实现jQuery Bluimp File Upload控件(来自此处: https : //github.com/blueimp/jQuery-File-Upload ),但是有点麻烦。 The backend will eventually support mobile apps, so it is already enabled for CORS via this (in the Register method of WebApiConfig ): 后端最终将支持移动应用程序,因此已经通过以下方式在CORS中启用了它(在WebApiConfigRegister方法中):

config.EnableCors(new EnableCorsAttribute("*", "*", "*"));

The entire backend is locked down with an authentication handler that is overriding the SendAsync method to look at request.Headers.Authorization , pull out the parameter and authenticate the request which is where my issue lies. 整个后端都使用身份验证处理程序锁定,该处理程序将重写SendAsync方法以查看request.Headers.Authorization ,拉出参数并对请求进行身份验证,这就是我的问题所在。 No matter what I try (including from this SO link: jQuery-File-Upload by blueimp - additional headers ), I can't get the control to send the headers to the backend, thereby preventing me from authenticating the upload request before allowing it to save. 无论我尝试什么(包括通过此SO链接: blueimp的jQuery-File-Upload-其他标头 ),我都无法获得将标头发送到后端的控件,从而阻止了我在允许上传请求之前对其进行身份验证保存。 The various methods I've tried are: 我尝试过的各种方法是:

        $(function() {
          $('#fileupload').fileupload({
            add: function(e, data) {
              data.headers = {
                'Authorization': 'Basic ' + $.cookie('auth')
              };
              data.submit();
            },
            headers: {
              Authorization: 'Basic ' + $.cookie('auth')
            },
            beforeSend: function(xhr) {
              xhr.setRequestHeader("Authorization", 'Basic ' + $.cookie('auth'));
            },
            xhrFields: {
              "Authorization": 'Basic ' + $.cookie('auth')
            },
            requestHeaders: {
              "Authorization": 'Basic ' + $.cookie('auth')
            },
            singleFileUploads: false,
            dataType: 'jsonp',
            url: window.core.get_serverUrl() + 'upload/uploadcasefile',
            done: function(e, data) {
              $.each(data.result.files, function(index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
              });
            }
          });
        });

Any ideas on how to force this thing to send the headers? 关于如何强制此事件发送标头的任何想法? Ultimately I'm just trying to implement a drag/drop file upload and don't even need most of the features of this control, but it seemed like it had some nice options. 最终,我只是尝试实现拖放文件的上传,甚至不需要此控件的大多数功能,但似乎它有一些不错的选择。 If I could just get it to send the headers, I think I'd be set. 如果我能发送标题,那我就定了。

This may be a little old however, people may run into this in the future. 这可能有点旧,但是将来人们可能会遇到这种情况。 There is an option for including headers. 有一个包括标题的选项。 It is treated just like all of the other options: ie all that is needed is update options with the headers you would like in the "headers" object on the options object. 就像所有其他选项一样对待它:即,所需的只是带有选项的更新选项,这些选项带有您希望在options对象的“ headers”对象中使用的标题。

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

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