This is rails 3.0.x. I was just wondering how to update an attribute of a model's photo/avatar directly without prompting the user to upload from his/her system?
I am using Paperclip to attach a photo to a model; It has the extension so that a user can use a URL to upload a photo. This works.
Now, I was looking at Canvas2Image, and it returns an image of the canvas so user's can download them.
I was wondering how to use the URL that Canvas2Image returns so I can update the current model's photo directly?
For now, I have this form that prompts the user to update the current model's avatar/photo:
<% form_for(@prototype,
:url=>{:controller=>"prototypes",
:action=>"update"},
:html => { :multipart => true }) do |f| %>
<%= f.file_field :avatar %><br><br>
<%= f.text_field :image_url %><br><br>
<%= submit_tag "Save Prototype" %>
<% end %>
Any help is appreciated, thanks!
From your link to Canvas2Image above it looks to me like you are trying to handle Base64-encoded images (like data:image/octet-stream;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wB
).
Paperclip expects a Tempfile
object, which is created behind the scenes for you when you upload an image. It is possible to fake a Tempfile
by using a StringIO
object and adding content_type
and original_filename
instance methods.
I posted a gist with the code I used to accomplish this in one of my projects. https://gist.github.com/1012107.
For you to do something similar you'd need to remove the beginning portion of the Canvas2Image URIs data:image/octet-stream;base64,
and use the remaining Base64 code as the image_data. You'd need a way to provide or work around the content-type and filename.
If you just want to use normal URLs like http://example.com/image.jpg
then you have several available options.
You could simply save the URL and use it as an image source on your pages. I know this isn't always an option, and you have no control over the availability of the image.
Otherwise, there are several utilities such as net/http and curl that you could use to download the image. If you think you want to go this route then you might also consider downloading the images using delayed jobs or a similar background process. That way your UI doesn't hang if the image download is slow.