久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何將文件從 HTML5 拖放到 Rails 3 應(yīng)用程序和回形

How to POST files from HTML5 Drag-Drop to a Rails 3 App amp; Paperclip?(如何將文件從 HTML5 拖放到 Rails 3 應(yīng)用程序和回形針?)
本文介紹了如何將文件從 HTML5 拖放到 Rails 3 應(yīng)用程序和回形針?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試在帶有 Paperclip 的 Rails 3 應(yīng)用程序中獲得一些 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 中的新附件

現(xiàn)在,我可以讓它工作的唯一方法是發(fā)送一個帶有文件數(shù)據(jù)的 XMLHttpRequest 并讓我的 Rails 操作讀取 request.raw_post ...這不是一個可行的解決方案,因?yàn)槲倚枰l(fā)送額外的 POST參數(shù)和真實(shí)性令牌.

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

那么,使用附加參數(shù)將拖放文件發(fā)布到我的應(yīng)用程序的正確方法是什么?

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

(如果有幫助,我將整個實(shí)驗(yàn)作為 這里的公共 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 上).這可能正是您正在尋找的內(nèi)容,包括有關(guān)如何將它與 Rails 3 和回形針一起使用的教程.根據(jù)我自己的經(jīng)驗(yàn),它就像一種魅力.

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結(jié)合的一個很好的例子

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

這篇關(guān)于如何將文件從 HTML5 拖放到 Rails 3 應(yīng)用程序和回形針?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認(rèn)為文檔“準(zhǔn)備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發(fā)技術(shù)
What do jasmine runs and waitsFor actually do?(jasmine 運(yùn)行和等待實(shí)際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈?zhǔn)椒椒ㄟM(jìn)行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監(jiān)視函數(shù)中的函數(shù)調(diào)用?)
主站蜘蛛池模板: 精品亚洲91 | 久久国产精品精品 | 亚洲欧美日韩成人在线 | 最新日韩在线视频 | 亚洲精品字幕 | 国产美女精品 | 国产精品夜夜春夜夜爽久久电影 | 亚洲视频在线一区 | 国产不卡一区在线观看 | 91视频大全 | 久久国内| 婷婷久久综合 | 国产一在线| 2018中文字幕第一页 | 高清黄色 | 国产精品嫩草影院精东 | 搞黄网站在线观看 | 精品1区2区3区 | 欧美日韩一二区 | 欧美成人h版在线观看 | 日韩精品av一区二区三区 | 国产在线观看一区二区三区 | 欧美一区二区三区在线观看视频 | 国产精品视频在线观看 | 亚洲日本欧美日韩高观看 | 亚洲444kkkk在线观看最新 | 国产免费观看久久黄av片涩av | 日本不卡一区 | 亚洲天堂精品一区 | 中文字幕日本一区二区 | 狠狠操电影 | 欧美 中文字幕 | 毛片一区二区三区 | 国产精品国产成人国产三级 | 国产黄色av电影 | 亚洲精品一区二区二区 | 国产在线高清 | 99精品国产一区二区青青牛奶 | 亚洲一区中文 | 国产视频久久 | 成人av一区二区三区 |