REF: Upload of Files

180 views
Skip to first unread message

Teddy Nyambe

unread,
Mar 20, 2013, 8:30:29 AM3/20/13
to web2py
I want to upload a file to web2py and then manipulate it. I dont want to save it into the database...after manipulating it i want to delete it. How do i do that. I have been trying like this but all fails:

Controller:

def quotes():
    form = SQLFORM.factory(Field("quotes", "upload")
    if form.process().accepted:
        manage_quotes(form.vars.quotes)
    elif form.errors:
        response.flash = "form has error"

After upload I cannot see the uploaded in the upload folder!

regards



--
.......................................................................................
Teddy Lubasi Nyambe
Opensource Zambia
Lusaka, ZAMBIA

Cell: +260 97 7760473
website: http://www.opensource.org.zm

~/
Human Knowledge belongs to the world! - AntiTrust

Man is a tool-using animal. Without tools he is nothing, with tools he is all - Thomas Carlyle 1795-1881

/~

Anthony

unread,
Mar 20, 2013, 9:21:18 AM3/20/13
to web...@googlegroups.com
As is, your code should generate an error because you haven't specified an upload folder, which is required when passing new Field objects to SQLFORM.factory. Try:

    import os
    SQLFORM
.factory(Field("quotes", "upload", uploadfolder=os.path.join(request.folder, 'uploads')))

If you don't want to save the file at all, though, then don't call form.process(). Instead, just check for the uploaded file:

    if request.vars.quotes:
        manage_quotes
(request.vars.quotes.file)

Anthony

Teddy Nyambe

unread,
Mar 20, 2013, 10:06:28 AM3/20/13
to web2py
Anthony,

Am having a very funny behavior. I am loading a form in a component quotes.load in another page of a <div>.
When i access the component directly through the URL address the {{=BEAUTIFY(request.vars)}} am able to see the uploaded file, but accessing it through the component its sending empty quotes upload file note the code:

controller

def quotes():
    form = SQLFORM.factory(Field("first_name"), Field("quotes", "upload", uploadfolder=os.path.join(request.folder,'uploads'))) 

    file_name="" 

    first_name = "" 

    if request.vars.quotes:
          file_name="test" 

    if request.vars.first_name:
        first_name = "Testing" 

    return dict(form=form, file_name = file_name, first_name = first_name)

 

 

view::-------->>>>>>>

First name is: {{=file_name}}<br> 

        {{=form}}<br> 

        First name is:{{=first_name}} 

filename is returning empty all the time.



--
 
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Anthony

unread,
Mar 20, 2013, 10:23:41 AM3/20/13
to web...@googlegroups.com
Did you set ajax=True or ajax_trap=True in your LOAD() call? If not, the form will get posted to the action of the containing page, not the component action.

Anthony

Teddy Nyambe

unread,
Mar 20, 2013, 3:01:01 PM3/20/13
to web2py

Actually this this is how am loading it, please advise:

{{=A('Quotes', component=URL('quotes','quotes.loadp ), target='quote-content')}}

<div id='quote-content'></div>

One funny thing is when I add a simple text field to the SQLFORM.factory and submit sample content...its displayed...only for upload is not showing.

Teddy Nyambe

unread,
Mar 21, 2013, 2:39:07 AM3/21/13
to web2py
Anthony,

I have noticed an error in the code. I have created a very simple form to test the upload without using components. The code is as follows:

Controller:
=============
def index():
    form = SQLFORM.factory(Field("first_name"),Field("quotes_file", "upload", uploadfolder=os.path.join(request.folder, 'uploads')))
    test = ''
    first_name = ''
    if request.vars.first_name:
        first_name = "My test"
    if request.vars.quotes_file:
        test = "My File"
    return locals()

View:
==============
{{extend 'layout.html'}}<br>
File: {{=test}}<br>

First name: {{=first_name}}
{{=form}}
{{=BEAUTIFY(request.vars)}}

When i submit the form with a file attached the beautify displays the file content and the value for first_name.

But, the value for "test" = "My File" is not being displayed in the view, while the "first_name" = "My test" is being displayed without any problem. 

I have further noticed this error in the page source after making a submit:

<script type="text/javascript"><!--
// These variables are used by the web2py_ajax_init function in web2py_ajax.js (which is loaded below).
var w2p_ajax_confirm_message = "Are you sure you want to delete this object?";
var w2p_ajax_date_format = "%Y-%m-%d";
var w2p_ajax_datetime_format = "%Y-%m-%d %H:%M:%S";
var ajax_error_500 = 'An error occured, please <a href="/testapp/default/index?first_name=bggfbfg&amp;quotes_file=FieldStorage%28%27quotes_file%27%2C+%27rows+%282%29.csv%27%2C+%27circular.circular_no%2Ccircular.recipients%2Ccircular.circular_title%2Ccircular.circular_date%5Cr%5CnNAZ%2F1%2F2013%2CALL+MEMBERS+OF+STAFF%2CLunch+to+be+served+at+the+Motel%2C2013-03-01%5Cr%5Cn%27%29">reload</a> the page'
//--></script>

any idea on whats going on here?

Anthony

unread,
Mar 21, 2013, 12:48:51 PM3/21/13
to web...@googlegroups.com
    if request.vars.quotes_file:

Sorry, I didn't realize this, but apparently if you do "if [cgi.FieldStorage object]" you get False rather than True, so instead try:

if hasattr(request.vars.quotes_file, "file"):

or

import cgi
if isinstance(request.vars.quotes_file, cgi.FieldStorage)

 
I have further noticed this error in the page source after making a submit:
 
var ajax_error_500 = 'An error occured, please <a href="/testapp/default/index?first_name=bggfbfg&amp;quotes_file=FieldStorage%28%27quotes_file%27%2C+%27rows+%282%29.csv%27%2C+%27circular.circular_no%2Ccircular.recipients%2Ccircular.circular_title%2Ccircular.circular_date%5Cr%5CnNAZ%2F1%2F2013%2CALL+MEMBERS+OF+STAFF%2CLunch+to+be+served+at+the+Motel%2C2013-03-01%5Cr%5Cn%27%29">reload</a> the page'
 
That is not an error -- it is just a Javascript variable that is automatically generated by web2py_ajax.html. It is there in case there is an Ajax error on the page, in which case it will be used to generate an error message. However, I notice there is an error in the code used to generate that variable -- it should not be including post_vars in the link URL, only get_vars (I have submitted a patch to correct that).

Anthony

Teddy Nyambe

unread,
Mar 21, 2013, 2:01:01 PM3/21/13
to web2py

But the question again is where is the file stored, its not appearing in the specified uploads directory for manipulation. What I want to do is upload an excel sheet with specific columns of data, then get the data and insert into db with similar columns

--

Anthony

unread,
Mar 21, 2013, 7:21:06 PM3/21/13
to web...@googlegroups.com
Recall that the suggestions to forego calling form.process() and instead just check for and directly manipulate request.vars.quotes_file was assuming you did not want to permanently store the file. If you do want to keep the file, then you should call form.process() (or include code to manually save the file). However, it's not clear that you do really need to keep the file. Presumably whatever code you plan to use to process and store the data would first open the file and then work with the open file object -- instead, you should be able to work with the FieldStorage file object directly (i.e., request.vars.quotes_file.file).

Anthony

Teddy Nyambe

unread,
Mar 22, 2013, 2:34:16 AM3/22/13
to web2py
Ok great and thanks for all your help, the file is being saved and all works as expected!!! code that works below:

def index():
    form = SQLFORM.factory(Field("first_name"),Field("quotes_file", "upload", uploadfolder=os.path.join(request.folder, 'uploads')))
    test = ''
    first_name = ''
    if request.vars.first_name:
        first_name = "My test"
    if isinstance(request.vars.quotes_file, cgi.FieldStorage):
        test = "My File"
    if form.process().accepted:
        response.flash = "Data Saved"
    elif form.errors:
        response.flash = "Form has error"
    return locals()

wwwgong

unread,
Mar 23, 2013, 2:52:32 PM3/23/13
to web...@googlegroups.com
Thanks for sharing the solution, I did not know that quotes_file holds the file content
Reply all
Reply to author
Forward
0 new messages