我使用这个脚本在Rails 3.2.8应用程序中使用
HTML5 FormData上传文件(逐个).
$('.uploader input:file').on('change',function() { $this = $(this); $('.alert').remove(); $.each($this[0].files,function(key,file) { $('.files').append('<li>' + file.name + '</li>'); data = new FormData(); data.append(file.name,file); $.ajax({ url: $('.uploader').attr('action'),contentType: 'multipart/form-data',type: 'POST',dataType: 'json',data: data,processData: false }); }); });
webrick/server.rb:191:in `block in start_thread' ERROR ArgumentError: invalid %-encoding ("filename.jpeg" Content-Type: image/jpeg
解决方法
你见过这个问题吗?
Sending multipart/formdata with jQuery.ajax
看起来您可能会遇到添加内容类型标头的jQuery,这会导致边界字符串丢失.从以上链接的问题:
It’s imperative that you set the
contentType
option tofalse
,forcing jQuery not to add a Content-Type header for you,otherwise,the boundary string will be missing from it. Also,you must leave theprocessData
flag set tofalse
,jQuery will try to convert your FormData into a string,which will fail.
基于此,尝试一下:
$.ajax({ url: $('.uploader').attr('action'),contentType: false,cache: false,processData: false,data: data });
我自己没试过,但我怀疑这可能是你正在寻找的机器人:)