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

发布于 2024-12-27 14:34:01 字数 2230 浏览 1 评论 0原文

当我通过下面的代码通过 plupload 上传文件时,在 Firebug 控制台中我看到消息 POST /uploads 200 OK 8192ms。文本的颜色是红色。当我查看终端输出时,有 Completed 200 OK in 7653ms

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();

上传控制器中,创建操作如下所示:

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

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

如何重定向到任何页面?可以看到,我尝试在完成上传到页面用户后进行重定向,但不幸的是没有发生任何事情。如果在 create 操作中的 head 200 行中,那么也没有发生任何事情。

谁能帮助我,上传完成后如何重定向到任何其他页面?我试图在谷歌上搜索,但我没有找到任何方法......

我还想问你 - 为什么在上传文件后总是在 Firebug 控制台中显示行 POST /uploads 200 OK 没有任何日志消息?

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. The color of the text is red. When I take a look to the terminal output, there is Completed 200 OK in 7653ms.

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:

  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.

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我可以简单地使用 window.location javascript 函数来做到这一点。 Plupload 有一个 uploadcomplete 事件,该事件会生成当所有文件上传完成后。 FileUploaded 事件在单个文件上传后生成。因此,如果您希望上传多个文件,uploadcomplete 事件会很有帮助上传者的想法取自 此处并添加了上传完成事件。这是我的上传者。

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

上传完成后,上传者会重定向以列出所有已上传的文档。

I can simply do it with window.location javascript function. Plupload has a uploadcomplete Event, which get generated when all the files uploading is complete. FileUploaded event is generated after a single file upload. So uploadcomplete event is helpful if u wish to upload multiple file The uploader idea is taken from here and added upload complete event. 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.

葬心 2025-01-03 14:34:01

两个选项:

1) Plupload 有一个可供您使用的 FileUploaded 回调:

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

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

Two options:

1) Plupload has a FileUploaded callback that you can use:

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:

  # 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
奶气 2025-01-03 14:34:01

您无法以这种方式进行重定向,因为使用任何上传运行时上传文件都是间接操作。 HTML5 使用 XHR,HTML4 使用代理 iframe,flash/silverlight 使用其内置的字节流 API 来执行上传。

如果您一次只上传一个文件,则唯一的选择是使用 FileUploaded 事件。第三个参数由所有运行时的服务器响应填充。您实际上会像这样使用它(从 iWasRobbed 编辑的 copypasta):

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

响应将始终是纯文本,因为它是 plupload 唯一可以在几个不同的运行时标准化的东西,因此您的重定向 URL 必须位于响应的正文。如果您需要更复杂的响应,您始终可以发送序列化 JSON 并在客户端解析它。

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.

If you are just uploading one file at a time, your only option is to use the FileUploaded event. The third parameter gets populated with the server response from all runtimes. You would actually use it like this (edited copypasta from iWasRobbed):

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. If you need more complex responses you can always send serialized JSON and parse it on the client side.

星星的轨迹 2025-01-03 14:34:01

我知道这有点旧,但我想改进响应。根据 @gonchuki 所说,我按照我的观点这样做了:

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

在我的控制器中,我只是用简单的文本渲染 url:

def create
  # do stuff here
  render :text => object_path(id: object)
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:

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

And in my controller I just render the url with simple text:

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

Hope this help too :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文