Rails: Upload a File or Store a Url

Rails: upload a file OR store a url

What I will probably do is to have a drop down in the file upload form to select if the file is from the disk or from external url.

By default it will set to "from disk", and if they select external url, you could use some AJAX magic and hide the file upload text box and have a text box to use the external url/script etc..

in the table, you can keep another two columns,

1 - external url

2 - file category (external / uploaded file)

by that way you can distinguish the files and how they what to display in the view

HTH

Uploading File into Server and store the path in database in Ruby on Rails

db/migrate/20110711000004_create_files.rb

class CreateFiles < ActiveRecord::Migration
def change
create_table :files do |t|
t.string :name
# If using MySQL, blobs default to 64k, so we have to give
# an explicit size to extend them
t.binary :data, :limit => 1.megabyte
end
end
end

app/controllers/upload_controller.rb

 class UploadController < ApplicationController
def get
@file = File.new
end
end

app/views/upload/get.html.erb

<% form_for(:file,
url: {action: 'save'},
html: {multipart: true}) do |form| %>
Upload your file: <%= form.file_field("uploaded_file") %><br/>
<%= submit_tag("Upload file") %>
<% end %>

app/models/file.rb

class File < ActiveRecord::Base
def uploaded_file=(file_field)
self.name = base_part_of(file_field.original_filename)
self.data = file_field.read
end
def base_part_of(file_name)
File.basename(file_name).gsub(/[^\w._-]/, '')
end
end

app/controllers/upload_controller.rb

def save
@file = File.new(params[:file])
if @file.save
redirect_to(action: 'show', id: @file.id)
else
render(action: :get)
end
end

app/controllers/upload_controller.rb

def file
@file = File.find(params[:id])
send_data(@File.data,
filename: @File.name,
disposition: "inline")
end

app/controllers/upload_controller.rb

def show
@file = File.find(params[:id])
end

app/views/upload/show.html.erb

<h3><%= @file.name %></h3>
<img src="<%= url_for(:action => 'file', :id => @file.id) %>"/>

Best way to have Upload File or Insert URL option

Paperclip does all of this for you. I've used it several times and highly recommend it.

How to save an image from a url with rails active storage?

Just found the answer to my own question. My first instinct was pretty close...

require 'open-uri'

class User < ApplicationRecord
has_one_attached :avatar
before_save :grab_image

def grab_image
downloaded_image = open("http://www.example.com/image.jpg")
self.avatar.attach(io: downloaded_image , filename: "foo.jpg")
end

end

Update: please note comment below, "you have to be careful not to pass user input to open, it can execute arbitrary code, e.g. open("|date")"

CarrierWave how to store file at given url

I've had lots of trouble trying to figure out how to get store! to work with local file paths. It turns out that store! actually takes a file as a parameter, not a string.

For the URL, you'll need to require 'open-uri' first, then open the file/url. Something like this should work:

require 'open-uri'
gravatar_url = "http://www.gravatar.com/avatar/#{Digest::MD5.new.update(current_user.email)}?s=512&d=identicon"
tempfile = open(gravatar_url)

uploader = ImageUploader.new
uploader.store! tempfile

The same will work with a file path, but you don't have to require open-uri in that case.

How to upload image in ruby app folder and insert url in database column

User(Model) mount_uploader :userImage, AvatarUploader

UsersController -> @user = User.new(user_params)
if @user.save
redirect_to(:action => 'show', id: User.last.id)
else
render :json => data_hash2, :content_type => 'application/json'
end

class AvatarUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end end

If You Using Web Service

<form action="http://ruby/controllername/create" lass="new_user" id="new_user" enctype="multipart/form-data" accept-charset="UTF-8" method="post">

Directly download from a link and upload file to GCS

Google Cloud Storage does not offer compute features. That means you cannot directly load an object into Cloud Storage from a URL. You must fetch the object and then upload it into Cloud Storage.



Related Topics



Leave a reply



Submit