SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: File upload

  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File upload

    We have a list of job numbers that are sent round to all the departments as a printed sheet. The program that generates the print out can also generate comma delimited text files (csv). It would be neat if I could allow the user creating the csv file, to upload it to our intranet, and have a bit of code that reads through the file and generated a list from it (and perhaps populates a MySQL table from the data). I think I can create the code that validates that the file is of the correct format, reads through the data, and populates a table. I have created the form and controller to upload the file. What I can't work out is how to programmatically grab the file and use it for processing.

    The examples I can find on the web and in the Pragmatic Ruby on Rails books seem to be picture file specific. I can't seem to find a more generic example.

    Basically what I want is to have a form with an input like:
    Code:
    <input type="file" name="jobnumbers[file]" />
    And to have code in a model that starts with code that:

    Assigns the file uploaded as jobnumbers[file] to a new File object "file"

    Can anyone suggest what I need to do? What is the name of the object that holds a file once it has been uploaded? Am I doing something wrong at the input?

    The file is being uploaded, but I can't work out the syntax of the code I need to grab it and start processing it. Am I right to try putting such code in the Jobnumber model?

  2. #2
    SitePoint Enthusiast Stevenwulf's Avatar
    Join Date
    May 2002
    Location
    Berkeley
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Reggie,

    It sounds like putting the code in the Jobnumber Model is appropriate. I'm doing somthing similar and I found that it is really easy to use the file_column plugin. Basically, once you have the plugin installed, you specify in your model that a particular column is a file:

    Code:
     file_column :file  
    #:file is the name of the column that represents the file
    Anyway, when you upload the file and save the model, the plugin will store the path of the file in the column. In you case it could be accessed by jobnumber.file

    So if you wanted to parse the file after it was saved

    f = File.open(jobnumber.file)
    f.readlines

    Anyway, the file_column site has more info.
    http://www.kanthak.net/opensource/file_column/

    -Steven

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Steven. You got me on the right path!

    I've had a couple of other jobs to deal with so have only just got back to here.
    1. Installed the file_column plugin into my application's plugin folder.
    2. Created a MySQL table with id, file_name (varchar), created_at (datetime) fields and called the table jobcodefiles.
    3. Created a model:
      Code:
      class Jobcodefile < ActiveRecord::Base
      	file_column :file_name
      end
    4. Created a controller:
      Code:
      class JobcodefileController < ApplicationController
      	def new
      		if request.get?
      			@jobcodefile = Jobcodefile.new
      		else
      			@jobcodefile = Jobcodefile.new(params[:jobcodefile])
      			@jobcodefile.save
      		end
      	end
      end
    5. Created a view (new.rhtml)
      Code:
      <%= form_tag({:action => 'new'}, :multipart => true) %>
        <p>Upload file:
        <%= file_field("jobcodefile", "file_name") %>
        </p>
        <%= submit_tag("Upload file") %>
      <%= end_form_tag %>

    When I submitted the first file via the form, the file was uploaded to:

    ..\public\jobcodefile\file_name\1\

    The full path and file name to that file are available as Jobcodefile.file_name

    Splendid. That gives me just the starting point I need.

    Thanks again.

    Reggie
    Last edited by ReggieB; Mar 31, 2006 at 01:43.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •