Plupload错误消息

时间:2011-08-27 21:16:02

标签: javascript ruby-on-rails ajax plupload

我正在使用带有rails应用程序的Plupload。上传器允许用户提交图像,但我要求图像大于某个最小像素大小。在处理从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的错误消息。

我感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

多个上传运营商plupload

 #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>