【问题标题】:Send Dropzone file.fullPath to Rails controller将 Dropzone file.fullPath 发送到 Rails 控制器
【发布时间】:2015-04-02 22:11:54
【问题描述】:

我正在尝试从将文件夹拖放到 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

我想访问这个:

上传控制器

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

Routes.rb

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

【问题讨论】:

    标签: javascript jquery ruby-on-rails ajax


    【解决方案1】:

    这是有效的:

    添加到 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);
    });
    

    【讨论】:

    • +1 但我不需要将 uploadDropzone 设置为 false 即可。您的sending 回调对我很有帮助。
    猜你喜欢
    • 2016-01-09
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多