# Store file from file\_field

**URL:** <https://www.ruby-forum.com/t/store-file-from-file-field/144500>\
**Category:** Rails\
**Created:** [July 30, 2008, 11:27am UTC](https://www.ruby-forum.com/t/store-file-from-file-field/144500 "2008-07-30T11:27:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [July 30, 2008, 11:27am UTC](https://www.ruby-forum.com/t/store-file-from-file-field/144500/1 "2008-07-30T11:27:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [January 7, 2009, 5:59pm UTC](https://www.ruby-forum.com/t/store-file-from-file-field/144500/2 "2009-01-07T17:59:02Z")

</div>

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-
