2015-04-02 127 views
0

我想提取文件的完整路径,这是从Chrome中将文件夹放入Dropzone的上传文件。文件上传和所有其他元数据正在正确传输。我只想传递正在上传的文件的完整路径。发送Dropzone file.fullPath到Rails控制器

我已经尝试了很多来解决这个问题,但仍然相当新,尤其是对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 

我想访问这个在:

上传控制器

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 

回答

3

这是什么工作:

增加的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); 
}); 
+0

+1,但我并不需要设置'uploadDropzone为了这个工作,把它变成虚假的。你的'发送'回调对我来说很有魅力。 – starfry 2016-02-11 08:40:46