JQuery-File-Upload客户端图像使用coffeescript调整大小

时间:2014-01-27 22:12:15

标签: javascript ruby-on-rails coffeescript jquery-file-upload

我想使用Jquery-File-Upload实现客户端图片大小调整:

https://github.com/blueimp/jQuery-File-Upload/wiki/Client-side-Image-Resizing

我想在coffeescript中实现它,以便使用我从这个railscast中找到的一些文件上传代码:

http://railscasts.com/episodes/383-uploading-to-amazon-s3

我不确定如何将disableImageResize示例与coffeescript结合使用。这就是我所拥有的,它不起作用(我可以运行应用程序,但它似乎没有做任何调整大小):

jQuery ->
  $('#new_image').fileupload
    dataType: "script"     
    disableImageResize: /Android(?!.*Chrome)|Opera/
                .test(window.navigator && navigator.userAgent),
            imageMaxWidth: 667,
            imageMaxHeight: 667
    add: (e, data) ->
      types = /(\.|\/)(gif|jpe?g|png)$/i
      file = data.files[0]
      if types.test(file.type) || types.test(file.name)
        $('#images').append('<div class="placeholder"></div>');
        $('.placeholder').hide();
        data.context = $(tmpl("template-upload", file))
        $('#imageUploadProgress').append(data.context)
        data.submit()       
      else
        alert("#{file.name} is not a gif, jpeg, or png image file")
    progress: (e, data) ->
      if data.context
        progress = parseInt(data.loaded / data.total * 100, 10)
        data.context.find('.bar').css('width', progress + '%')
        if progress == 100
           $('.placeholder').show();
           # scroll to the bottom of the thumbGallery to show recently uploaded image
           document.getElementById("images").scrollTop = document.getElementById("images").scrollHeight

有人可以告诉我我做错了什么吗?我发现了有关同一主题的其他StackOverflow问题,但两者都没有答案:

How to resize images client side using jquery file upload

Resizing image at client-side using JQuery file upload to amazon S3

1 个答案:

答案 0 :(得分:1)

简单的方法是删除add回调。否则,您负责手动触发调整大小,我还没有找到该怎么做!