问题
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