John Smith
John Smith

Reputation: 6269

Cannot upload file

I have problems to upload a file:

Here is my html:

<%= form_tag import_test_path, multipart: true do %>
 <%= file_field_tag :file, :style => 'display:inline; margin-top:-10px' %>
 <%= submit_tag 'Hochladen', :class => 'btn btn-sm btn-info' %>
<% end %> 

First i tried to simply upload it and to handle it in my controller like this:

  def import
    widgets = DBF::Table.new(params[:file], nil, 'cp1252')
    w = widgets.find(6)
    p = Patient.new
    p.vorname = w.vorname
    p.name = w.name
    p.geburtsdatum = w.geburt
    p.save
    respond_to do |format|
     format.html {redirect_to :back }
    end  
  end

But this provoced an error:

 no implicit conversion of ActionDispatch::Http::UploadedFile into String
 in line: DBF::Table.new(params[:file], nil, 'cp1252')

Next i tried to generate first an Tempfile:

def import
    file = Tempfile.new(params[:file])
    widgets = DBF::Table.new(file, nil, 'cp1252')
    w = widgets.find(6)
    p = Patient.new
    p.vorname = w.vorname
    p.name = w.name
    p.geburtsdatum = w.geburt
    p.save
    respond_to do |format|
     format.html {redirect_to :back }
    end  
  end

But this also proved an error:

 unexpected prefix_suffix: #<ActionDispatch::Http::UploadedFile:0x6793d10 @tempfile=#  <Tempfile:C:/Users/EMMANU~1/AppData/Local/Temp/RackMultipart20131110-6816-ogkd3i>, @original_filename="patient.DBF", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"patient.DBF\"\r\nContent-Type: application/octet-stream\r\n">
 in line: file = Tempfile.new(params[:file])

What do i wrong? Thanks and have a nice day!

Upvotes: 1

Views: 3107

Answers (2)

Roman Savchak
Roman Savchak

Reputation: 1

Try DBF::Table.new(params[:file].path, nil, 'cp1252')

Upvotes: 0

phoet
phoet

Reputation: 18845

Accessing params[:file] is a ActionDispatch::Http::UploadedFile which is just a wrapper for the TmpFile that is used to store the uploaded content.

You need to read from that IO like object to get the contents.

Upvotes: 3

Related Questions