Forum OpenACS Q&A: Using "file" widget with ad_form

Collapse
Posted by Dave Bauer on
I can use the file widget to accept an uploaded file, and I can collect the client upload filename.

How do I get the temp_filename and mime_type properties from the widget?

Collapse
Posted by Dave Bauer on
ok so far I have this:

{file_var:file(file)}

which gives me a file datatype with a file widget.

I am now getting a list with 3 elements, but the tmp_filename and mime type are still blank. I don't really care about mime_type, but I still need to read the file.

Collapse
Posted by Dave Bauer on
I needed to declare the form like this:
ad_form -name my_form -html { enctype multipart/form-data } - form {
    {file_var:file(file) {label "Upload file"}}
}
Then i used template::util::file::get_property to get the tmp_filename.
Collapse
Posted by Tom Jackson on

I seem to be using the following:

  upload_file:trim,notnull
  upload_file.tmpfile:tmpfile
....

# then in the body of the tcl page:

set file_id [open ${upload_file.tmpfile} r]

...

but I needed to read using ns_getcsv, so if you want to copy it just use 'file copy ${upload_file.tmpfile} newfilename'

Collapse
Posted by Dave Bauer on
Actually I am using the new found oacs_util::csv_foreach to process a csv file also.

I did not need any declaration of the file variable in ad_page _contract although I can probably use that for validation of the types of variables.

Collapse
Posted by Tom Jackson on

Shoot, I should just read the subject, I'm an ad_form dunce, so it sounds like you figured it out? Or not?

Collapse
Posted by Jun Yamog on
Hi Dave,

Use this on ad_form

    {upload_file:file(file),optional {label "File"}}

Then on -edit_data or somewhere else on ad_form use this

    # get the file properties
    set upload_filename [template::util::file::get_property filename $upload_file]
    set upload_tmpfile [template::util::file::get_property tmp_filename $upload_file]
    set upload_mime_type [template::util::file::get_property mime_type $upload_file]

You can also see my code in file-upload.tcl, it uses the wizard, ad_form, etc.

Collapse
Posted by Jun Yamog on
Hmmm.... I think you have already answered your question.  The tmp file should be the one you can open/read.

I did something similar where in the uploaded file is an essay and the rules of the essay says "essay should have no more than 500 words".  I had to open the file and count the words on -validate :).