ruby on rails file upload and download

[color=indigo]
To upload in RubyOnRails, just copy the following code to the specified location. 1. Add def uploadFile(file)
in application.rb    if !file.original_filename.empty?    @filename=getFileName(file.original_filename) File.open    ("#{RAILS_ROOT}/public/images/#{@filename}" , "wb") do |f|    f.write(file.read)    end    return @filename end end def getFileName(filename) if !filename.nil? return filename end end Second, define the save Action in your defined controller , the specific code is as follows: def save unless request.get? if filename=uploadFile(params[:file]['file'])    render :text=>filename end end end






















3. Finally, you only need to write the file form in the view:
<% form_tag ({:action=>"save"},:multipart=>true ) do%>
<input type="file" name="file [file]">
<input type="submit">
<% end %>[color=indigo]
[/color]
[/color]

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326680318&siteId=291194637