如何将文件从 HTML5 拖放到 Rails 3 应用程序和回形针?

How to POST files from HTML5 Drag-Drop to a Rails 3 App amp; Paperclip?(如何将文件从 HTML5 拖放到 Rails 3 应用程序和回形针?)
本文介绍了如何将文件从 HTML5 拖放到 Rails 3 应用程序和回形针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试在带有 Paperclip 的 Rails 3 应用程序中获得一些 html5 拖放功能.所以,基本上:

I'm trying to get some html5 drag-and-drop functionality in a Rails 3 app with Paperclip. So, basically:

  1. 一个或多个文件被拖放到一个 DIV 上
  2. 文件被 POST 到 Rails 动作(一起或一次一个)
  3. Rails 操作将每个文件保存为 Paperclip 中的新附件

现在,我可以让它工作的唯一方法是发送一个带有文件数据的 XMLHttpRequest 并让我的 Rails 操作读取 request.raw_post ...这不是一个可行的解决方案,因为我需要发送额外的 POST参数和真实性令牌.

Right now the only way I can get this working is by sending an XMLHttpRequest with the File data and having my Rails action read the request.raw_post ... this isn't a workable solution because I need to send along additional POST params and the authenticity token.

这是我目前所拥有的:

<!-- IN MY VIEW -->
<h2>Drag and drop upload</h2>

<div id="drop">
  <h2>Drop Files Here</h2>
</div>

<script type="text/javascript" charset="utf-8">
  var dropbox = document.getElementById("drop");  
  drop = function(evt) {   
    evt.stopPropagation();
    evt.preventDefault();
    var files = evt.dataTransfer.files;
    var count = files.length;
    if (count > 0) {
        // handleFiles(files);
      var url = '/images/raw';
      var request = new XMLHttpRequest();
      request.open("POST",  url, true); // open asynchronous post request
      request.send(files[0]);
    }
  }
  dragEnter = function(evt) {
    evt.stopPropagation();
    evt.preventDefault();
  }
  dragExit = function(evt) {
    evt.stopPropagation();
    evt.preventDefault();
  }
  dragOver = function(evt) {
    evt.stopPropagation();
    evt.preventDefault();
  }
  // init event handlers
  dropbox.addEventListener("dragenter", dragEnter, false);
  dropbox.addEventListener("dragexit", dragExit, false);
  dropbox.addEventListener("dragover", dragOver, false);
  dropbox.addEventListener("drop", drop, false);
</script>

还有我的控制器操作:

class ImagesController < ApplicationController

  # ... Normal REST actions 

  def raw
    name = "tmp_image.png"
    data = request.raw_post
    @file_content = File.open("#{Rails.root.to_s}/tmp/#{name}", "wb") do |f| 
      f.write(data)
    end
    @image = Image.new(:attachment => File.new("#{Rails.root.to_s}/tmp/#{name}"))
    @image.save
    File.unlink("#{Rails.root.to_s}/tmp/#{name}")
    render :text => 'success'    
  end
end

那么,使用附加参数将拖放文件发布到我的应用程序的正确方法是什么?

So, what's the proper way to POST drag-and-drop files to my app with additional params?

(如果有帮助,我将整个实验作为 这里的公共 GitHub 存储库)

(If it helps, I have the entire experiment as a public GitHub repo here)

推荐答案

看看

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

然后向下滚动到 Ruby(在 Rails 上).这可能正是您正在寻找的内容,包括有关如何将它与 Rails 3 和回形针一起使用的教程.根据我自己的经验,它就像一种魅力.

and scroll down to Ruby (on Rails). That probably is exactly what you are looking for, including a tutorial on how to use it with Rails 3 and paperclip. And from my own experiences it works like a charm.

正如 Joost 所说:https://github.com/yortz/carrierwave_jquery_file_upload展示了如何将carrierwave与jquery_file_upload结合的一个很好的例子

And as Joost commented: https://github.com/yortz/carrierwave_jquery_file_upload shows a nice example of how to combine carrierwave with jquery_file_upload

这篇关于如何将文件从 HTML5 拖放到 Rails 3 应用程序和回形针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)