I have a data uri which I want to upload to the server and store it as a paperclip image attachment. So I used the info on this link to convert the data uri into a blob and then used FormData to upload it to the server. My coffeescript file looks like this
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,
});
And this is the create action in my controller
image = params[:image]
name = SecureRandom.hex + "png"
File.open("#{Rails.root}/public/uploads/#{name}", 'wb') do |f|
f.write(image.read)
end
This works perfectly for storing the image on the server. But how do I integrate it with paperclip, i.e. to generate thumbnails, urls for these images? Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire