ActiveStorage to upload large base64 encoded string?

社会主义新天地 提交于 2019-12-22 07:00:04

问题


If I have an image that was edited/generated using JavaScript on the client (for example, a cropped photo or the result of a canvas drawing), is there a way to upload it using ActiveStorage?

It would typically be a large string containing "<img src='data:image/jpeg;base64,...=='>" that is stored in a JavaScript variable, not a file.


回答1:


As far as I know, Active Storage has currently no native support for that.

Maybe this Rails issue has further information helpful to you.

We have implemented Data URI uploads with Shrine (and its DataURI Plugin) and are waiting until there is a proper way to do this with Active Storage before we will migrate.




回答2:


You can do something like this:

decoded_data = Base64.decode64(params[:base64].split(',')[1])
resource.image = { 
  io: StringIO.new(decoded_data),
  content_type: 'image/jpeg',
  filename: 'image.jpg'
}


来源:https://stackoverflow.com/questions/50140171/activestorage-to-upload-large-base64-encoded-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!