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:
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