本文介绍了jQuery File Upload,指定FormData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下jQuery File Upload插件:

I'm working to use the following jQuery File Upload plugin:

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

我需要特定的额外formdata,它说有一个选项,但是我遇到了JS错误未捕获的SyntaxError:意外的标识符",并且没有FormData示例,这使得工作变得很困难.

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.

这是我所拥有的:

$(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 %>')
     }
   ]
    });
});

推荐答案

您在formData的正确位置没有逗号,我想您希望它像这样:

You don't have commas in the right places in your formData, I think you want it to be like this:

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 %>')
    }
]

请注意,name: ...部分后面有逗号,而value: ...部分后面没有逗号.

Note that there are commas after the name: ... parts but not the value: ... parts.

此外,我不认为encodeURIComponent()是这里合适的转义/编码机制,并且<%= u ...已经URI编码.您需要做的就是确保字符串不包含未转义的单引号,这样类似的事情可能会起作用(假设此JavaScript正在通过ERB):

Also, I don't think encodeURIComponent() is the appropriate escaping/encoding mechanism here and <%= u ... already URI encodes. 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):

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

适当的编码应由插件处理,并且几乎可以肯定它正在执行POST,因此URL编码甚至不适用.

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.

此外,您不需要在JavaScript字符串中转义斜线,因为它们不是特殊的,所以您可以在'</td>'所在的地方说'<\/td>'.

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>'.

我对jQuery-File-Upload一无所知,但修复逗号至少应该使您摆脱眼前的问题(以及新的和更有趣的问题!).

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,指定FormData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:27