加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用RubyOnRails上传HTML5 FormData文件

发布时间:2020-12-17 04:29:27 所属栏目:百科 来源:网络整理
导读:我使用这个脚本在Rails 3.2.8应用程序中使用 HTML5 FormData上传文件(逐个). http://jsfiddle.net/RamPr/ $('.uploader input:file').on('change',function() { $this = $(this); $('.alert').remove(); $.each($this[0].files,function(key,file) { $('.fil
我使用这个脚本在Rails 3.2.8应用程序中使用 HTML5 FormData上传文件(逐个).

http://jsfiddle.net/RamPr/

$('.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 to false,forcing jQuery not to add a Content-Type header for you,otherwise,the boundary string will be missing from it. Also,you must leave the processData flag set to false,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
});

我自己没试过,但我怀疑这可能是你正在寻找的机器人:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读