微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 如何在AJAX中将RecordRTC blob文件上传到Rails回形针

在客户端,用户使用 RecordRTC来录制短视频.当用户按下上传时,我将使用recorder.getBlob()获取视频的blob数据,并将其上传到我的服务器(使用Rails和 paperclip来处理文件上传).

首先,我想更改< input type ='file'> blob数据的字段值.事实证明,对于浏览器的安全性,我无法使用javascript更改它.

然后,我尝试使用AJAX:

$("#ajax-submit").on("click",function() {
    var data = new FormData();

    data.append("record",recorder.getBlob(),(new Date()).getTime() + ".webm");

    var oReq = new XMLHttpRequest();
    oReq.open("POST","/records/");
    oReq.send(data);
    oReq.onload = function(oEvent) {
        if (oReq.status == 200) {
            console.log("Uploaded");
        } else {
            console.log("Error " + oReq.status + " occurred uploading your file.");
        }
    };
});

但是,它不起作用.在日志文件中,我将得到以下,无法处理:

Processing by RecordsController#create as */*
Parameters: {
  "video"=>"data:video/webm;base64,GkXfo0AgQoaBAUL3gQFC8o..."
}

如果我正常使用表单提交,我将有如下参数:

Processing by RecordsController#create as HTML
Parameters: {
    "video"=>#<Actiondispatch::Http::UploadedFile:0x3b476e0 @original_filename="testing.mp4",@content_type="video/mp4",@headers="Content-disposition: form-data; name=\"record\"; filename=\"testing.mp4\"\r\nContent-Type: video/mp4\r\n",@tempfile=#<File:a_path>>
}

我怎么能解决这个问题?

非常感谢.

解决方法

我在我的一个项目中处理了同样的问题.情况是API必须将blob数据转换为移动设备发送的图像文件.我假设要在您的控制器文件上传操作.
def upload
  #extract the video data from params
  video = params[:video]

  # define the save path for the video. I am using public directory for the moment. 
  save_path = Rails.root.join("public/videos/#{video.original_filename}")

  # Open and write the file to file system.
  File.open(save_path,'wb') do |f|
    f.write params[:video].read
  end

  render :nothing => true
end

原文地址:https://www.jb51.cc/ruby/274255.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐