我在用blueimp jquery 文件上传在 Rails 3.2 应用程序中,通过jquery-文件上传-rails gem.
我试图在上传之前在客户端调整图像大小,但在遵循文档时遇到问题。我的代码如下。目前上传工作正常,但图像大小未调整。
通过 jquery-file-upload 调整图像大小的正确语法是什么?
(coffeescript 中显示的两种方法基于this and this文档。两者都不适合我。)
#Coffeescript
jQuery ->
if $("#new_asset").length
$("#new_asset").fileupload
dataType: "script"
add: (e, data) ->
types = /(\.|\/)(jpe?g|png)$/i
file = data.files[0]
if types.test(file.type) || types.test(file.name)
data.context = $(tmpl("template-upload", file))
$('#progress-container').append(data.context)
jqXHR = data.submit()
$("button.cancel").click (e) ->
jqXHR.abort()
else
alert("#{file.name} is not a 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 + '%')
stop: (e, data) ->
$('.upload').hide()
process: [
action: "load"
fileTypes: /^image\/(gif|jp?g)$/
maxFileSize: 20000000 # 20MB
,
action: "resize"
imageMaxWidth: 1500
imageMaxHeight: 1500
,
action: "save"
]
dropZone: $(".dropzone")
sequentialUploads: true
disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator and navigator.userAgent)
imageMaxWidth: 1500
imageMaxHeight: 1500
downloadTemplateId: null
#application.js
//= require jquery-fileupload
EDIT
根据马坦萨的回答,add
我的代码中的回调会阻止自动调用任何处理函数。所以我想我需要做类似的事情
...
add: (e, data) ->
$.each data.result, (index, file) ->
// processing code goes here
但我在找出正确的语法或理解可用的指南时遇到了很多麻烦。
如何在添加回调中对每个文件应用调整大小处理?