简体   繁体   English

jQuery文件上传,指定FormData

[英]jQuery File Upload, specifying FormData

I'm working to use the following jQuery File Upload plugin: 我正在使用以下jQuery文件上传插件:

https://github.com/blueimp/jQuery-File-Upload/wiki/Options

I need to specific extra formdata which it says there is an option for, but I'm getting a JS error "Uncaught SyntaxError: Unexpected identifier" and there are no FormData examples, which is making it hard to get to work. 我需要特定的额外formdata,它说有一个选项,但我得到一个JS错误“Uncaught SyntaxError:Unexpected identifier”,并且没有FormData示例,这使得很难开始工作。

Here is what i have: 这是我有的:

$(function () {
    $('.upload').fileUploadUI({
        uploadTable: $('.upload_files'),
        downloadTable: $('.download_files'),
        buildUploadRow: function (files, index) {
            var file = files[index];
            return $(
                '<tr>' +
                '<td>' + file.name + '<\/td>' +
                '<td class="file_upload_progress"><div><\/div><\/td>' +
                '<td class="file_upload_cancel">' +
                '<div class="ui-state-default ui-corner-all ui-state-hover" title="Cancel">' +
                '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
                '<\/div>' +
                '<\/td>' +
                '<\/tr>'
            );
        },
        buildDownloadRow: function (file) {
            return $(
                '<tr><td>' + file.name + ' ' + file.type + ' ' + file.size + '<\/td><\/tr>'
            );
        },
  formData: 
   [
     {
       name: '_http_accept'
       value: 'application/javascript'
     },
     {
       name: '<%= session_key_name %>'
       value: encodeURIComponent('<%= u cookies[session_key_name] %>'),
     },
     {
       name: 'authenticity_token'
       value: encodeURIComponent('<%= u form_authenticity_token %>')
     }
   ]
    });
});

You don't have commas in the right places in your formData , I think you want it to be like this: 你没有在formData中的正确位置使用逗号,我想你希望它是这样的:

formData: [
    {
        name: '_http_accept',
        value: 'application/javascript'   
    }, {
        name: '<%= session_key_name %>',
        value: encodeURIComponent('<%= u cookies[session_key_name] %>')    
    }, {
        name: 'authenticity_token',
        value: encodeURIComponent('<%= u form_authenticity_token %>')  
    }
]

Note that there are commas after the name: ... parts but not the value: ... parts. 请注意, name: ...后面有逗号name: ...部分但不是value: ...部分。

Also, I don't think encodeURIComponent() is the appropriate escaping/encoding mechanism here and <%= u ... already URI encodes. 另外,我认为encodeURIComponent()不是这里适当的转义/编码机制, <%= u ...已经是URI编码。 All you need to do is make sure the string doesn't contain an unescaped single quote so something more like this would probably work (assuming that this JavaScript is going through ERB): 你需要做的就是确保字符串不包含未转义的单引号,所以更像这样的东西可能会起作用(假设这个JavaScript正在通过ERB):

value: '<%= cookies[session_key_name].gsub(/'/, "\'") %>'

The appropriate encoding should be handled by the plugin and it is almost certainly doing a POST anyway so URL encoding doesn't even apply. 适当的编码应该由插件处理,并且几乎肯定会进行POST,因此URL编码甚至不适用。

Also, you don't need to escape slashes in JavaScript strings, they're not special so you can just say '</td>' where you say '<\\/td>' . 此外,你不需要在JavaScript字符串中转义斜杠,它们并不特别,所以你可以在'</td>'中说'<\\/td>'

I don't know anything about jQuery-File-Upload but fixing your commas should at least get you past your immediate problem (and on to new and more interesting problems!). 我对jQuery-File-Upload一无所知,但修复你的逗号至少应该让你超越你的直接问题(以及新的和更有趣的问题!)。

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

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