【发布时间】:2017-03-22 03:37:15
【问题描述】:
我正在关注 CarrierWave Wiki https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Add-more-files-and-remove-single-file-when-using-default-multiple-file-uploads-feature 中的这篇文章,以使用 CarrierWave 多次上传功能为我的系统中的模型添加更多图像和删除图像。
那篇文章的主要代码是
def add_more_images(new_images)
images = @gallery.images
images += new_images
@gallery.images = images
end
def remove_image_at_index(index)
remain_images = @gallery.images # copy the array
deleted_image = remain_images.delete_at(index) # delete the target image
deleted_image.try(:remove!) # delete image from S3
@gallery.images = remain_images # re-assign back
end
它有效。 不过,太慢了。我看过日志,整体处理时间如下:
- 上传 1 张图片:例如需要 5000 毫秒
- 再添加 1 张图片:需要 8500 毫秒(2 张图片)
- 再添加 1 张图片:需要 12000 毫秒(3 张图片)
- 删除 1 张图片:需要 8400 毫秒(返回 2 张图片)
我在本地机器上测试了作者编写的这个解决方案的示例应用程序,速度也很慢。
即使我们只添加或删除 1 张图片,CarrierWave 似乎也会重新上传和重新处理所有图片。我想是因为我们将新的图像数组重新分配给@gallery,以便它将旧图像视为新图像。
这里还有一个相关的问题https://github.com/carrierwaveuploader/carrierwave/issues/1704#issuecomment-259106600
有没有人有更好的解决方案来使用 CarrierWave 多次上传功能添加和删除图像?
谢谢。
【问题讨论】:
标签: ruby-on-rails ruby carrierwave image-uploading