Rails Carrierwave Base64图像上传 [英] Rails Carrierwave Base64 image upload

查看:67
本文介绍了Rails Carrierwave Base64图像上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Carrierwave将图像从客户端上传到Rails后端的最佳方法是什么。现在,我们的iOS开发人员正在以base64格式发送文件,因此请求如下所示:

  image_data => ;/ 9J / 4AAQSkZJRgABAQAAAQABAAD / 4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAHqADAAQAAAABAAAAHgAAAAD / 2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH / 2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH / wAARCAAeAB4DAREAAhEBAxEB / 8QAHwAAAQUBAQE .... 

所以,我的问题实际上是两个问题,我应该告诉他以其他文件格式发送吗?如果base64是发送这些文件的正确方法,那我该如何在载波中处理它们呢?

解决方案

我认为一种解决方案是将解码后的数据保存到文件中,然后将此文件分配给已挂载的上传器。然后,删除该文件。 / p>

其他(内存中)解决方案可以是ne:

 #定义类,该类使用carrierwave 
类所要求的方法扩展IO。 StringIO
def original_filename
#实名无所谓
photo.jpeg
end

def content_type
#这应该反映真正的内容类型,但在此示例中没问题
image / jpeg
结束
结束

#某些模型与载波上传器
class SomeModel
#上传者
mount_uploader:photo,PhotoUploader

#此方法将在控制器的标准分配过程中调用
#(如`update_attributes`)
def image_data = {data)
#解码数据并在其上创建流
io = CarrierStringIO.new(Base64.decode64(data))

#这样就可以了(照片已安装好载波上传器)
self.photo = io
结束

结束


What is the best way to upload an image from a client to a Rails backend using Carrierwave. Right now our iOS developer is sending in the files as base64, so the requests come in like this:

"image_data"=>"/9j/4AAQSkZJRgABAQAAAQABAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAHqADAAQAAAABAAAAHgAAAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAAeAB4DAREAAhEBAxEB/8QAHwAAAQUBAQE....

So, my question is really two questions. Should I tell him to send in a different file format? If base64 is the right way to send these files in, then how do I deal with them in carrierwave?

解决方案

I think that one solution can be to save the decoded data to file and then assign this file to mounted uploader. And after that get rid of that file.

The other (in-memory) solution can be this one:

# define class that extends IO with methods that are required by carrierwave
class CarrierStringIO < StringIO
  def original_filename
    # the real name does not matter
    "photo.jpeg"
  end

  def content_type
    # this should reflect real content type, but for this example it's ok
    "image/jpeg"
  end
end

# some model with carrierwave uploader
class SomeModel
  # the uploader
  mount_uploader :photo, PhotoUploader

  # this method will be called during standard assignment in your controller
  # (like `update_attributes`)
  def image_data=(data)
    # decode data and create stream on them
    io = CarrierStringIO.new(Base64.decode64(data))

    # this will do the thing (photo is mounted carrierwave uploader)
    self.photo = io
  end

end

这篇关于Rails Carrierwave Base64图像上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆