Vasanthakumar C. wrote:
Hi,
I did what all u said (uploading file to database & also directory)
but nothing worked out.
Error messages can be helpful…
Dont know where i am going wrong. Please check
this code below:-
I wrote it to upload a file to database:-
This code here writes it to filesystem not the database.
def update
@content = Content.find_by_id(params[:id])
@content.updatedBy = session[:user_id]
@content.updatedOn = Time.now
Just a little tip: if you have a column called updated_on or updated_at
rails will automatically set its values. Same is true for created_on /
created_at.
if params[:content][:picture] && params[:content][:picture].size > 0
We only want to upload a file if one exists in the parameters. This
checks that the param we want exists and make sures that it actually has
data in it (".size > 0" meaning they uploaded a file instead of just
left the file field blank)
File.open("#{RAILS_ROOT}/images/#{params[:content][:picture]}",
'wb+') do |f|
Since we are writing a file, we need to open it first. The first
argument is the path on your web server to the file. Make sure to start
it with the RAILS_ROOT constant to ensure that you are writing file with
paths relative to your home directory. This path would yield something
like:
/home/my_rails_app/images/123
The second argument is the file mode, in this case ‘wb+’ broken into its
parts means:
‘w’ : open file for writing
‘b’ : in binary mode
‘+’ : create file if it doesnt it.
f.write params[:content][:picture].read
Now that we are inside the open file block we can write to the file
itself with file.write. You can get the uploaded file data as a string
with the read method. So we write the file to disk by reading the
uploaded file.
end
end
@content.save
end
def image
send_file “#{RAILS_ROOT}/images/#{params[:id]}”, :disposition =>
‘inline’
end
I just blindly put the code u gave… dont know what it does. whats the
method “def image” for?? it is not called anywhere - is it automatically
called??
“image” is an action in the controller that is called just like any
other action. Except instead returning HTML, it returns binary data.
send_file will make rails open a file anywhere on your web server and
then send its contents in response to the request. So use it your view
to place these images.
<%= image_tag(:action => ‘image’, :id => 123) %>
Alternatively, you can upload the file to your public directory, in
which case you don’t need to have an image action at all, just link to
the the publicly accessible file.
<%= image_tag(’/images/123.jpg’) %>
Please help me out…
I hope that I just did. And I once again recommend using flex image as
this sort of thing can be kind of a pain. With a FlexImage::Model class
you can simply assign the binary upload the same as you would any other
column. Assuming you have uploaded a file into params[:content][:data]
and any other attributes in the params[:content] hash
Content is a FlexImage::Model class
Content.create(params[:content])
So you dont have to write any code that treats the binary column
different that the other data in your model.
I also want to point out a minor caveat. You must use “:multipart =>
true” on your form_tag. Otherwise no binary data will be sent with your
form submission.
<%= form_tag({:action => ‘upload_file’}, {:multipart => true}) %>
Good luck,
-Alex