简体   繁体   English

Rails 3和plupload-上传后如何重定向?

[英]Rails 3 and plupload - how can I do redirect after upload?

When I upload an file by the plupload by the code below, so in the firebug console I see the message POST /uploads 200 OK 8192ms . 当我通过以下代码通过plupload上传文件时,因此在Firebug控制台中,我看到消息POST / uploads 200 OK 8192ms The color of the text is red. 文字颜色为红色。 When I take a look to the terminal output, there is Completed 200 OK in 7653ms . 当我看一下终端输出时, 在7653ms内Completed 200 OK

var uploader = new plupload.Uploader({
    runtimes: 'gears,html5,flash,silverlight,browserplus',
    browse_button: 'pickfiles',    
    autostart : true,    
    max_file_size: '10mb',
    url: '/uploads',
    resize: { width: 320, height: 240, quality: 90 },
    flash_swf_url: '/Scripts/pl/plupload.flash.swf',
    silverlight_xap_url: '/Scripts/pl/plupload.silverlight.xap',
    filters: [
    { title: "Image files", extensions: "jpg,gif,png" },
    { title: "Zip files", extensions: "zip" }
]
});
uploader.bind('Init', function (up, params) {
    $('#filelist')[0].innerHTML = "<div>Current runtime: " + params.runtime + "</div>";
});
uploader.bind('Error', function (up, err) {
    $('#filelist').append("<div>Error: " + err.code +
        ", Message: " + err.message +
        (err.file ? ", File: " + err.file.name : "") +
        "</div>"
    );

});
uploader.bind('FilesAdded', function (up, files) {
    for (var i in files) {
        $('#filelist')[0].innerHTML += '<div id="' + files[i].id + '">' + files[i].name + ' (' + plupload.formatSize(files[i].size) + ') <b></b></div>';
    }
    //uploader.start();
});
$('#uploadfiles').click(function (e) {
    uploader.start();
    e.preventDefault();
});

uploader.bind('UploadProgress', function (up, file) {
    $('#' + file.id)[0].getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
});

uploader.init();

In the Uploads controller the action create looks this: Uploads控制器中,操作创建如下:

  def create
    @upload = Upload.new(:upload => params[:file])

    if @upload.save
      head 200
      #redirect_to '/users'
    else
      render :action => "new"
    end
  end

How can I make a redirect to any page? 如何重定向到任何页面? As is possible to see, I tried to make a redirect after finish an upload to the page users , but unfortunately nothing happend. 可以看到,在完成向页面用户的上传后,我尝试进行重定向,但是很遗憾,没有任何反应。 If is in the create action the line head 200 , so also nothing happend. 如果在创建动作中是head 200 ,那么也什么也没发生。

Could anyone help me, please, how can I make a redirect to any other page after finish upload? 任何人都可以帮助我,完成上传后如何重定向到其他页面? I tried to search at google, but I didn't find any way to do it... 我试图在Google上进行搜索,但找不到任何方法...

And I would like to ask you yet - why is always in the Firebug console after uploaded file the line POST /uploads 200 OK without any log message? 我想问你-为什么在上传文件后,总是在Firebug控制台中显示POST / uploads 200 OK,而没有任何日志消息?

I can simply do it with window.location javascript function. 我可以简单地使用window.location javascript函数来做到这一点。 Plupload has a uploadcomplete Event, which get generated when all the files uploading is complete. Plupload有一个uploadcomplete事件,该事件在所有文件上传完成时生成。 FileUploaded event is generated after a single file upload. 上传单个文件后,将生成FileUploaded事件。 So uploadcomplete event is helpful if u wish to upload multiple file The uploader idea is taken from here and added upload complete event. 因此,如果您希望上传多个文件,则uploadcomplete事件将很有帮助。上传者的想法是从此处获取的,并添加了upload complete事件。 Here is my uploader. 这是我的上传者。

<script type="text/javascript">
 $(function(){
  var uploader = new plupload.Uploader({
   runtimes : "html5",
   browse_button : 'pickfiles',
   max_file_size : '100mb',
   url : "/documents",
   multipart: true,
   multipart_params: {
   "authenticity_token" : '<%= form_authenticity_token %>'
  }
});

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

    );

  });


   });

  uploader.bind('UploadProgress', function(up, file) {    

$('#' + file.id + " b").html(file.percent + "%");


  });

  uploader.bind('UploadComplete', function(up, files) {
  window.location = '/documents';
 });


  $('#uploadfiles').click(function(e) {
  uploader.start();
  e.preventDefault();
  });


  uploader.init();


  });


  </script>

After the upload is complete the uploader redirects to list all the uploaded documents. 上传完成后,上传者将重定向以列出所有上传的文档。

Two options: 两种选择:

1) Plupload has a FileUploaded callback that you can use: 1)Plupload有一个FileUploaded回调,您可以使用:

uploader.bind('FileUploaded', function(up, file, info) {
  // Redirect after successful upload
  window.location = 'http://mysite.com/users';
});

2) You can also just try letting Rails do the redirect by putting this code in your ApplicationController : 2)您也可以尝试通过将以下代码放在ApplicationController让Rails进行重定向:

  # Allows redirecting for AJAX calls as well as normal calls
  def redirect_to(options = {}, response_status = {})
    if request.xhr?
      render(:update) {|page| page.redirect_to(options)}
    else
      super(options, response_status)
    end
  end

I know this is a little old, but I want to improve the responses. 我知道这有点老了,但我想改善回应。 Based on what @gonchuki said, I did this to my view: 根据@gonchuki所说的,我是这样做的:

uploader.bind('FileUploaded', function(up, file, info) {
  window.location = info.response;
});

And in my controller I just render the url with simple text: 在我的控制器中,我只用简单的文本呈现url:

def create
  # do stuff here
  render :text => object_path(id: object)
end

Hope this help too :) 希望也能提供帮助:)

You cannot do a redirect on that way since uploading files with any upload runtime is an indirect action . 您不能以这种方式进行重定向,因为使用任何上载运行时来上载文件都是间接操作 HTML5 uses XHR, HTML4 uses a proxy iframe, and flash/silverlight use their builtin byte streaming APIs to perform the upload. HTML5使用XHR,HTML4使用代理iframe,而flash / silverlight使用其内置的字节流API执行上传。

If you are just uploading one file at a time, your only option is to use the FileUploaded event. 如果您一次仅上传一个文件,则唯一的选择是使用FileUploaded事件。 The third parameter gets populated with the server response from all runtimes. 第三个参数由所有运行时的服务器响应填充。 You would actually use it like this (edited copypasta from iWasRobbed): 您实际上会像这样使用它(来自iWasRobbed的编辑copypasta):

uploader.bind('FileUploaded', function(up, file, info) {
  // Redirect after successful upload
  window.location = info.response;
});

the response will always be plain text given that it's the only thing plupload can standardize upon the several different runtimes, so your redirect URL must be in the body of the response. 由于plupload只能在几种不同的运行时上实现标准化,因此响应将始终为纯文本,因此您的重定向URL必须位于响应的正文中。 If you need more complex responses you can always send serialized JSON and parse it on the client side. 如果您需要更复杂的响应,则始终可以发送序列化的JSON并在客户端进行解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM