gpt4 book ai didi

ruby-on-rails - Rails PaperClip 拖放多个文件

转载 作者:可可西里 更新时间:2023-11-01 12:50:36 26 4
gpt4 key购买 nike

我正在使用 PaperClip 和 Rails 上传文件,它工作正常,但它想要实现允许上传多个文件的拖放文件上传。并且每个文件不应超过一定大小。

编辑:

这是我目前所拥有的,我已经创建了 javascript 部分。 但是我不知道如何创建 Controller 部分:

var $dropArea = $(".drop-area");

$dropArea.bind({
dragover: function () {
$(this).addClass('hover');
return false;
},
dragend: function () {
$(this).removeClass('hover');
return false;
},
drop: function (e) {
e = e || window.event;
e.preventDefault();

e = e.originalEvent || e;

var files = (e.files || e.dataTransfer.files);

var $img = $('<img src="" class="uploadPic" title="" alt="" />');
for (var i = 0; i < files.length; i++) {
(function (i) {
var reader = new FileReader();
reader.onload = function (event) {
var newImg = $img.clone().attr({
src: event.target.result,
title: (files[i].name),
alt: (files[i].name)
});

$("body").append(newImg);
};
reader.readAsDataURL(files[i]);

var xhr = new XMLHttpRequest();

var fd = new FormData();

fd.append(files[i].name, files[i]);

xhr.open("POST", 'url', true);
xhr.send(fd);
})(i);
}

return false;
}
});

这是基本的 Controller 部分: 定义创建 @image = Image.new(参数[:image])

    if @image.save
respond_to do |format|
format.html { redirect_to action: 'index', :notice => 'Image saved'}
format.js { redirect_to action: 'index', :notice => 'Image saved'}
format.xml { redirect_to action: 'index', :notice => 'Image saved'}
end
else
flash[:notice] = "Error, Please try again"
redirect_to action: 'new'
end
end

我该怎么做?

谢谢

最佳答案

经过一些研究,我发现可以这样轻松地完成:

var dropArea = document.getElementById("droparea"),
viewArea = document.getElementById("previewarea");

dropArea.addEventListener("drop", function(evt){
evt.preventDefault();
evt.stopPropagation();

previewFiles(evt.dataTransfer.files);
return false;
}, false);

function previewFiles(files){
for (i=0; i < files.length; i++){
if (typeof FileReader != "undefined"){
var img = document.createElement("img");
viewArea.appendChild(img);

var reader = new FileReader();
reader.onload = (function(theImg){
return function(evt){
theImg.src = evt.target.result;
}
}(img));
reader.readAsDataURL(files[i]);
}
}

uploadFiles(files);
}

function uploadFiles(files){
var fd = FormData();
var position = 0;
var max = files.length;
if (typeof fd != "undefined"){
function queue(){
if (max >= 1 && position <= max - 1){
fd.append("file", files[position]);
upload();
}
}

function upload(){
$.ajax({
url: '/boxes/hiThere',
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
position = position + 1;
queue();
}
});
}
queue();
}
}

dropArea.addEventListener("dragenter", function(evt){
if (evt){
this.className = "drag-enter";
}
endFn(evt);
}, false);

dropArea.addEventListener("dragleave", function(evt){
this.className = "";
endFn(evt);
}, false);

dropArea.addEventListener("dragover", function(evt){
endFn(evt);
evt.dataTransfer.dropEffect = 'move';

return false;
}, false);

function endFn(evt){
evt.preventDefault();
evt.stopPropagation();
}

然后像这样简单地在 rails 中添加正常的保存:

def hiThere
box = Box.new(params[:box])
box.user_id = current_user.id
box.file_path = params[:file]

if box.save!
set_flash "File saved sucessfully"
else
set_flash "Error, please try again"
end

respond_to do |format|
format.js { redirect_to action: :index, :notice => "Done" }
end
end

关于ruby-on-rails - Rails PaperClip 拖放多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15703594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com