簡體   English   中英

使用上傳到Amazon S3的JQuery文件在客戶端調整圖像大小

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

我已經在互聯網上看到了一些示例,例如: thisthis ,但是我正在跟蹤RailsCast Episode#383 Uploading to Amazon S3 ,在視頻的最后,他提到了使用javascript調整客戶端圖像大小的操作。 , 當然。 問題是,如果按照他的榜樣,我將無法實施。 我正在使用gem jquery-fileupload-rails

編輯 :我注意到我在JQuery-fileupload示例中缺少一些JS 來調整圖像頁面的大小 ,但仍然無法正常工作。 我對application.js的要求

//= require jquery-fileupload/vendor/jquery.ui.widget
//= require jquery-fileupload/vendor/load-image
//= require jquery-fileupload/vendor/canvas-to-blob
//= require jquery-fileupload/jquery.iframe-transport
//= require jquery-fileupload/jquery.fileupload
//= require jquery-fileupload/vendor/tmpl
//= require jquery.fileupload-process
//= require jquery.fileupload-image
//= require jquery-fileupload/locale

我的posts.js.coffee http://pastebin.com/9sm7UtsP

jQuery ->
  $('#fileupload').fileupload
    add: (e, data) ->
      types = /(\.|\/)(gif|jpe?g|png)$/i
      file = data.files[0]
      if types.test(file.type) || types.test(file.name)
        data.context = $(tmpl("template-upload", file))
        $('#fileupload').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 + '%')

    done: (e, data) ->
      file = data.files[0]
      domain = $('#fileupload').attr('action')
      path = $('#fileupload input[name=key]').val().replace('${filename}', file.name)
      to = $('#fileupload').data('post')
      content = {}
      content[$('#fileupload').data('as')] = domain + path
      $.post(to, content)
      data.context.remove() if data.context # remove progress bar

    fail: (e, data) ->
      alert("#{data.files[0].name} failed to upload.")
      console.log("Upload failed:")
      console.log(data)

我的upload_helper.rb (路徑:apps / helpers) http://pastebin.com/VxAbiUft

module UploadHelper
  def s3_uploader_form(options = {}, &block)
    uploader = S3Uploader.new(options)
    form_tag(uploader.url, uploader.form_options) do
      uploader.fields.map do |name, value|
        hidden_field_tag(name, value)
      end.join.html_safe + capture(&block)
    end
  end

  class S3Uploader
    def initialize(options)
      @options = options.reverse_merge(
        id: "fileupload",
        aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"],
        aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
        bucket: ENV["AWS_S3_BUCKET"],
        acl: "public-read",
        expiration: 10.hours.from_now,
        max_file_size: 2.megabytes,
        as: "file"
      )
    end

    def form_options
      {
        id: @options[:id],
        method: "post",
        authenticity_token: false,
        multipart: true,
        data: {
          post: @options[:post],
          as: @options[:as]
        }
      }
    end

    def fields
      {
        :key => key,
        :acl => @options[:acl],
        :policy => policy,
        :signature => signature,
        "AWSAccessKeyId" => @options[:aws_access_key_id],
      }
    end

    def key
      @key ||= "uploads/#{SecureRandom.hex}/${filename}"
    end

    def url
      "https://#{@options[:bucket]}.s3.amazonaws.com/"
    end

    def policy
      Base64.encode64(policy_data.to_json).gsub("\n", "")
    end

    def policy_data
      {
        expiration: @options[:expiration],
        conditions: [
          ["starts-with", "$utf8", ""],
          ["starts-with", "$key", ""],
          ["content-length-range", 0, @options[:max_file_size]],
          {bucket: @options[:bucket]},
          {acl: @options[:acl]}
        ]
      }
    end

    def signature
      Base64.encode64(
        OpenSSL::HMAC.digest(
          OpenSSL::Digest::Digest.new('sha1'),
          @options[:aws_secret_access_key], policy
        )
      ).gsub("\n", "")
    end
  end
end

_form.html.erbhttp : //pastebin.com/Sqk8XK6U

create.js.erb@image變量來自控制器-我認為它與發布控制器邏輯無關,因為它是一個JavaScript問題) http://pastebin.com/BBAGE5Me

因此,我想在上載到S3之前調整圖像大小以創建縮略圖,這樣我就不會浪費我在S3上的存儲空間和客戶端的帶寬。 任何想法?

我已經嘗試將各個選項添加到posts.js.coffee腳本中: https : //github.com/blueimp/jQuery-File-Upload/wiki/Options但沒有成功。

如果您之前檢查過,就會發現其他類似的帖子。

似乎add回調導致process函數被忽略,因此您必須在add回調中手動調用它。 另外,您沒有定義任何用於調整代碼大小的選項...

我發布了一個類似的問題,並在玩了一會后自己解決了: 使用jquery fileupload與coffeescript-使用add回調時調整圖像大小

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM