2011-11-24 91 views
2

我想上傳與軌道True Type字體,但我recieving這個錯誤文件:錯誤上傳與軌道

Encoding::UndefinedConversionError in FontsController#create 
"\xE6" from ASCII-8BIT to UTF-8 

這裏是在控制器代碼:

def create 
    @font = Font.new(params[:font]) 
    upload = params[:upload] 
    name = upload['datafile'].original_filename 
    @font.font_type = File.extname(name) 
    @font.location = './public/fonts/' + name 

    puts "\n\n---------------------------\n#{upload['datafile'].class}\n-----------------------------\n\n" 

    File.open(@font.location, "w+") { |f| f.write(upload['datafile'].read) } 

    #Save the license 
    @font.save 

    respond_to do |format| 
     if @font.save 
     format.html { redirect_to(@font, :notice => 'Font was successfully created.') } 
     format.xml { render :xml => @font, :status => :created, :location => @font } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @font.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

,並在查看:

<%form_tag({:controller => 'fonts', :action=> 'create'}, {:multipart => true}) do%> 
    <% if @font.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@font.errors.count, "error") %> prohibited this font from being saved:</h2> 

     <ul> 
     <% @font.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

<%= file_field :upload, :datafile %> 

    <div class="actions"> 
      <%= submit_tag "Upload License" %> 
    </div> 
<% end %> 

另外,它會更容易使用一些其他方法的文件上傳...有沒有什麼好的寶石呢?由於

回答

7

寫的

File.open(@font.location, "wb") { |f| f.write(upload['datafile'].read) } 

代替

File.open(@font.location, "w+") { |f| f.write(upload['datafile'].read) } 

B模式以二進制方式打開該文件。

+0

+1我用ruby 1.9.2和rails 3.1得到了這個問題 –