I’ve made a file upload. When there is a file already on the server I
pass the values and bring them back to question if they want to
overwrite or cancel. I pass along the values with session values. But I
have problems with the file_field. How can I restore that? Can I add the
params[:document][:file_name] as a string and then add it to file_field
again?
Hi,
I am trying to do the same since I had problems using file_column.
I think I have the same issue : trying to update the file_field once a
file is already attached it seams not working.
Would be useful if you can paste you model.
mine is (its simply a cut&paste of what found in other sites)
def file=(uploaded_file)
unless uploaded_file == “”
@uploaded_file = uploaded_file
@filename = sanitize_filename(@uploaded_file.original_filename)
write_attribute(:content_type, @uploaded_file.content_type)
end
end
def after_create
unless @uploaded_file.nil?
FileUtils.mkdir_p(File.dirname(self.path)) unless File.exists?
(File.dirname(self.path))
if @uploaded_file.instance_of?(Tempfile)
FileUtils.copy(@uploaded_file.local_path, self.path)
else
File.open(self.path, “wb”) { |f| f.write(@uploaded_file.read) }
end
write_attribute(:file, self.simple_path) # vedi def simple path
self.save
end
end
def simple_path # Definisce cosa scrivere sul DB
“#{@filename}”
end
def path
File.expand_path("#{DIR}/public/files/#{self.id}/#{@filename}")
end
def after_destroy
route = “#{DIR}public/files/#{self.id}/#{self.file}”
folder = “#{DIR}public/files/#{self.id}/”
File.delete(route)
Dir.rmdir(folder)
end
private
def sanitize_filename(file_name)
get only the filename, not the whole path (from IE)
just_filename = File.basename(file_name.gsub("\", “/”)) # work-
around for IE
replace all none alphanumeric, underscore or perioids with
underscore
just_filename.gsub!(/[^a-zA-Z0-9.-+]/,"")
just_filename = “_#{just_filename}” if just_filename =~ /^.+$/
just_filename = “unnamed” if just_filename.size == 0
just_filename
end
On 30 Lug 2008, 10:27, Pål Bergström <rails-mailing-l…@andreas-