【问题标题】:Save a blob object with paperclip用回形针保存 blob 对象
【发布时间】:2015-04-25 14:39:02
【问题描述】:

我有一个数据 uri,我想将其上传到服务器并将其存储为回形针图像附件。所以我使用link 上的信息将数据 uri 转换为 blob,然后使用 FormData 将其上传到服务器。 我的咖啡脚本文件是这样的

dataURLtoBlob = (dataURL) ->
  binary = atob dataURL.split(',')[1]
  array = []
  i = 0
  while i < binary.length
    array.push binary.charCodeAt(i)
    i++
  return new Blob [new Uint8Array(array)], {type: 'image/png'}

file = dataURLtoBlob(imageSrc)
fd = new FormData()
fd.append "image", file
$.ajax({
   url: "/posts",
   type: "POST",
   data: fd,
   processData: false,
   contentType: false,
});

这是我控制器中的创建操作

image = params[:image]
name = SecureRandom.hex + "png"
File.open("#{Rails.root}/public/uploads/#{name}", 'wb') do |f|
  f.write(image.read)
end

这非常适合将图像存储在服务器上。但是如何将它与回形针集成,即为这些图像生成缩略图、url?提前致谢。

【问题讨论】:

标签: ruby-on-rails paperclip blob


【解决方案1】:

我也想到了同样的事情。

检查一下回形针数据库可能会更好,这对我来说很好。

https://github.com/softace/paperclip_database

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    相关资源
    最近更新 更多