简体   繁体   中英

How can i retrieve uploaded image (base64) full url from Carrierwave upload file to Rackspace?

I just need to get the url of my image (base64) that just uploaded to Rackspace server via Carrierwave.

This is my controller now.

def update_with_image
    user = current_user
    uploader = PictureUploader.new
    uploader.store!(user_update_params[:profile_image]) // base64 image like this 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2w...'

    // How can i update user_update_params[:profile_image] with the successfully uploaded profile_image url?

    if user.update_attributes(user_update_params)
      # Handle a successful update.
      render json: user, status: 200 ,serializer: UserSerializer
    else
      render json: { errors: user.errors }, status: 422
    end
  end

So after uploader.store!(user_update_params[:profile_image]) how can i get the url of that file?

Thanks!

You mean this ?

uploader = AvatarUploader.new
uploader.store!(my_file)                              # size: 1024x768

uploader.url # => '/url/to/my_file.png'               # size: 800x600
uploader.thumb.url # => '/url/to/thumb_my_file.png'   # size: 200x200

calling url method on the uploader should get you the URL. github

Update: Quoting from carrier wave GitHub

You can optionally include your CDN host name in the configuration. This is highly recommended, as without it every request requires a lookup of this information.

 config.asset_host = "http://c000000.cdn.rackspacecloud.com"

In your uploader, set the storage to :fog

class AvatarUploader < CarrierWave::Uploader::Base
  storage :fog
end

That's it! You can still use the CarrierWave::Uploader#url method to return the url to the file on Rackspace Cloud Files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM