Hi Everyone,
Atlast, i managed somehow to upload a file and read it. Here is
my code, dont know y "read" method didn''t work for me.
Controller:
def update
@content = Content.find_by_id(params[:id])
@content.updatedBy = session[:user_id]
@content.updatedOn = Time.now
File.delete(@content.picture) if File.exist?(@content.picture)
fileName = sanitize_filename(params[:content][:picture])
if params[:content][:picture] && params[:content][:picture].size
> 0
File.open("#{RAILS_ROOT}/uploads/#{fileName}",
''wb+'') do |f|
# f.write(params[:content][:picture].read)
@fileData = ""
f1=File.open(params[:content][:picture],''r'') do |f1|
while(line=f1.gets)
@fileData=@fileData+line
end
end
f.write(@fileData)
end
end
params[:content][:picture] = "#{RAILS_ROOT}/uploads/#{fileName}"
if @content.update_attributes(params[:content])
redirect_to :action => ''index''
else
render :action => ''edit''
end
end
def sanitize_filename(file_name)
# get only the filename, not the whole path (from IE)
just_filename = File.basename(file_name)
# replace all non-alphanumeric, underscore or periods with
underscores
just_filename.gsub(/[^\w\.\-]/,''_'')
end
View:
<img src="<%= content.picture %>" border="1"
width="100px"
height="75px">
Now, one more problem i got :-( the above code is working well if i
uploaded "text" file and read it manually. But the same is not going
well for "image" file.
The actual file size is around "120kb" but the uploaded file size
is only "2kb - 4 kb". Also i couldn''t even see a bit of the
image....
Can anyone help me.... Please....
Thanks in advance
Regards,
Vasanth
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Hi Alex,
I have done that already... now the problem i face is something
different. I cant display the image uploaded. See my post above.
Please anyone help me...
Thanks in advance
Regards,
Vasanth
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Some suggestions: 1. Try file_column. It''s very easy. 2. For your solution, try and render the page with the image, and then view the source of the rendered page in the browser. Take the url that is in the src= attribute, and paste it into your browser. That way you can determine if it''s a problem where you are placing the file in the filesystem, or if it is just a link issue. 3. If you use updated_on or updated_at in your field of your table, rails will set it for you automatically. updatedOn is not recognized by rails. 4. It looks like you are putting a full filesystem path into the src. This will not work, as your web server usually operates from a given directory. I can''t quite tell. I would really recommend using file_column. It will reduce all your code above to a couple of lines. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---