2

我正在将 Plupload 与 rails 应用程序一起使用。上传器允许用户提交图像,但我要求图像大于某个最小像素大小。这在处理从 Plupload 接收到的图像时在服务器端进行验证。

现在,上传器工作正常,但是当特定文件失败时,用户会看到一个橙色三角形,如果他们将鼠标悬停在它上面,则会收到“HTTP 错误”。这对普通用户来说是毫无用处的。

这就是我现在在 Rails 控制器中的内容:

def create
    @photo = current_user.photos.new(:image => params[:file])
    if @photo.save
        head :ok
    else
        head :bad_request
    end
end

如何将描述性错误消息从我的应用程序传递回 Plupload,以便它们可以在橙色三角形中显示给用户?

我是否需要返回一些 json 或 xml,或者不同的东西?我已经尝试返回失败图像对象的不同表示,但到目前为止,我所做的任何事情都没有让 Plupload 接收到我的错误消息。

我很感激任何帮助!

4

1 回答 1

0

多重上传载体插件

 #app/controllers/adm/galerias_imagem.rb
    class Adm::GaleriasImagensController < ApplicationController
    layout "adm_layout"
    ....
     def create

      @adm_galerias_imagem = Adm::GaleriasImagem.new(adm_galerias_imagem_params)
        respond_to do |format|
          if @adm_galerias_imagem.save   
              head :ok
        else
            head :bad_request         
        end
      end

#app/views/adm/galerias_imagens/new.html.erb
<div id="filelist">Your browser doesn't have Flash, Silverlight or HTML5 support.</div>
<br />

<div id="container">
<a id="pickfiles" href="javascript:;">[Select files]</a> 
<a id="uploadfiles" href="javascript:;">[Upload files]</a>
</div>


<%= link_to 'Back', adm_galerias_imagens_path, class: "btn btn-default" %>

<% content_for :js do %>
$(function(){
  var uploader = new plupload.Uploader({
    runtimes : 'html5,flash,silverlight,html4',
    browse_button : 'pickfiles', // you can pass in id...
    container: document.getElementById('container'), // ... or DOM Element itself
    url : '<%= adm_galerias_imagens_path %>',
    multipart: true,
    max_file_size : '10mb',
    multipart_params: {
      "authenticity_token" : "<%= form_authenticity_token %>",
      '<%= Rails.application.config.session_options[:key] %>': '<%= request.session_options[:id] %>',
      "adm_galerias_imagem[galeria_id]": <%= params[:id] %>
    },
    flash_swf_url : '/assets/Moxie.swf',
    silverlight_xap_url : '/assets/Moxie.xap',
    file_data_name:"adm_galerias_imagem[imagem]",
    filters : {
      max_file_size : '10mb',
      mime_types: [
      {title : "Image files", extensions : "jpg,gif,png"},
      {title : "Zip files", extensions : "zip"}
      ]
    }
  });
uploader.bind('Init', function(up, params) {
  $('#filelist').html("<div>Current runtime: " + params.runtime + "</div>");
});
uploader.bind('FilesAdded', function(up, files) {
  $.each(files, function(i, file) {
    $('#filelist').append(
      '<div id="' + file.id + '">' +
      'File: ' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +'</div>'
      );
});
});
uploader.bind('UploadProgress', function(up, file) {
  $('#' + file.id + " b").html(file.percent + "%");
});
$('#uploadfiles').click(function(e) {
  uploader.start();
  e.preventDefault();
});
uploader.init();
});
<% end %> 

#app/views/layouts/adm_layout.html.erb

    <!--Scripts-->
        <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
        <script type="text/javascript">
            <%= yield :js %>
        </script>
于 2014-07-28T01:02:51.883 回答