gpt4 book ai didi

javascript - 将 Dropzone file.fullPath 发送到 Rails Controller

转载 作者:行者123 更新时间:2023-11-28 15:24:58 24 4
gpt4 key购买 nike

我正在尝试从 Chrome 中将文件夹拖放到 Dropzone 中来提取已上传文件的完整路径。文件上传和所有其他元数据正在正确传输。我只想传递正在上传的文件的完整路径。

我已经尝试了很多方法来解决这个问题,但仍然相当新,尤其是对于 Javascript。这些是我的资源:

Get uploaded file from Dropzone

Passing JQuery Datepicker Data to Rails Controller

Passing the variables from from jquery to rails controller

How to pass variables from AJAX form into controller?

Dropzone.js and full path for each file (没有响应)

这些是我的尝试:

上传 new.html.erb

<%= form_for([@parent, @upload], method: :post, html: { multipart: true, class: "dropzone", id: "upload-dropzone"}) do |f| %>

<% if @upload.errors.any? %>
<ul>
<% @upload.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= hidden_field_tag :full_path %>
<%= javascript_tag do %>
window.parent = '<%= @parent.id %>';
<% end %>

<% end %>

application.js

//= require jquery
//= require jquery_ujs
//= require dropzone
//= require turbolinks
//= require_tree .
// Dropzone.autoDiscover = false;

// $(document).ready(function(){
// var myDropzone = Dropzone.getElementById("upload-dropzone");
// new Dropzone(myDropzone);
// myDropzone.on("sending", function(file, xhr, formData){
// formData.append('full_path', file.fullPath);
// })
// console.log(file.fullPath);
// })

// var myDropzone = new Dropzone("#upload-dropzone");
// document.querySelector("#upload-dropzone").classList.add("dropzone");
// var myDropzone = document.getElementById("upload-dropzone");
// Dropzone.myDropzone.on("sending", function(file, xhr, formData){
// formData.append('full_path', file.fullPath);
// })

Dropzone.options.uploadDropzone = {

accept: function(file, done){
var fullPath = { full_path: file.fullPath };
$.ajax({
type: "POST",
data: { full_path: fullPath.full_path }
});
done();
}
}

application.js 还尝试了从 jQuery Dropzone 元素中提取 fullPath。在未注释的函数中,我可以使用以下命令访问 javascript 控制台中的完整路径:

file.fullPath

我想访问此:

上传 Controller

def create
@parent = Parent.find(params[:parent_id])
# here is an attempt to create the upload with the file_path
@upload = Upload.new(upload: params[:file], full_path: params[:full_path])
if @upload.save!

respond_to do |format|
format.json{ render json: @upload }
end
end
end

路线.rb

resources :parent do
post 'uploads/new' => 'uploads#new'
end

最佳答案

这就是有效的:

添加到application.js

Dropzone.options.uploadDropzone = false;

添加到upload.js

var myDropzone = new Dropzone("#upload-dropzone");
myDropzone.on("sending", function(file, xhr, formData) {
formData.append("full_path", file.fullPath);
});

关于javascript - 将 Dropzone file.fullPath 发送到 Rails Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29423361/

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