some questions:
Can I turnoff default changing name with upload function?
Can I make my upload manually, something like: brows_file ->
upload_to_some_folder.
What is happening after I submit button 'upload' - can I have control
on this activity.
I'm new with Web2Py and Python, but on PHP this is no problem, I make
all my code upload form and files go to folders what I chose, and file
name is my will same or changed...
In Python I see some functions os.rename for renaming files, but it is
directly rename file name.
Can I have may custom upload form with two way
first: sending file to folder - no need for database
second: sending data to database - and don'n need to know is file or
something else.
On download process I can us something like list folder content and
make links for download (from file list and database data I will
generate links for download)
If you have doubts why so complicated, and why I don't use simply way
shown in book -> answer is: the chairman of department what that files
stored as early mentioned...
Miroslav
in HTML:
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
in PHP file upload_file.php:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
Is this possible with Python and web2py?
Miroslav
in view:
{{=form}}
{{=form_data}}
in controller:
import cgi
def upload():
form = FORM("Upload
file:",INPUT(_type='file',_name='myfile'),INPUT(_type='submit',_name='submit',_value='Submit'))
if form.accepts(request,session):
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'please fill the form'
form_data = cgi.FieldStorage()
file_data = form['myfile']
fp =open('some/file','wb') // this folder/file make some errors
fp.write(file_data)
fp.close()
return dict(form=form,file_data=file_data)
This is what I have for now...
Miroslav
I reed all in online book about upload and forms...Can I make my upload manually, something like: brows_file ->
upload_to_some_folder.
What is happening after I submit button 'upload' - can I have control
on this activity.I'm new with Web2Py and Python, but on PHP this is no problem, I make
all my code upload form and files go to folders what I chose, and file
name is my will same or changed...
In Python I see some functions os.rename for renaming files, but it is
directly rename file name.Can I have may custom upload form with two way
first: sending file to folder - no need for database
second: sending data to database - and don'n need to know is file or
something else.On download process I can us something like list folder content and
make links for download (from file list and database data I will
generate links for download)If you have doubts why so complicated, and why I don't use simply way
shown in book -> answer is: the chairman of department what that files
stored as early mentioned...Miroslav
What I have for now:in view:
{{=form}}
{{=form_data}}
in controller:
import cgidef upload():
form = FORM("Upload
file:",INPUT(_type='file',_name='myfile'),INPUT(_type='submit',_name='submit',_value='Submit'))
if form.accepts(request,session):
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'please fill the form'
form_data = cgi.FieldStorage()
file_data = form['myfile']
fp =open('some/file','wb') // this folder/file make some errors
fp.write(file_data)
fp.close()
return dict(form=form,file_data=file_data)
I have error in this
form.vars.myfile.filename
<type 'exceptions.AttributeError'>('str' object has no attribute
'filename')
form.vars.myfile.file
<type 'exceptions.AttributeError'>('str' object has no attribute
'file')
Miroslav
How access to file before calling form? What that mean?
Miroslav
The error is caused when file is not selected.
By default on first run form is empty (file is not selected), and form
must wait for selecting and submitting.How access to file before calling form? What that mean?
Miroslav
request.vars.myfile -> make return on stored object
request.vars.myfile.file -> make return address of stored object
I understood what you told me, and logical check on conditions, but
how to put everything in one function.
the pseudo algorithm in my head is next:
make def func():
make form = FORM(...)
check if condition existing
make file = request.vars.myfile.file
make filename = request.vars.myfile.filename
make filepath = os.path.join(...)
make write file to filepath filename
go to page and show empty form - wait for new file
else:
just show empty form without submission
return form
but I just loss my mind after 24 ours of trying this or similar
uploads.
Miroslav
On Nov 19, 6:12 pm, Anthony <abas...@gmail.com> wrote:
> You can tell if the function is being called with a form submission by
> checking for request.vars:
>
> if request.vars:
> print 'this is a form submission'
>
>
>
>
>
>
>
> On Saturday, November 19, 2011 12:06:40 PM UTC-5, miroslavgojic wrote:
>
> > The error is caused when file is not selected.
> > By default on first run form is empty (file is not selected), and form
> > must wait for selecting and submitting.
>
> > How access to file before calling form? What that mean?
>
> > Miroslav
>
no difference functionally, just different APIs.
> If I have two variables (var1 and var2)
> var1 = Object
> var2 = cStringIO.StrindO 0x1234
>
> Before your example I was stack with
> file.write(var1)
> or
> file.write(var2)
> how I can get string value from var1=Object or var2=cStringIO.StrindO
file.write(var2.read())
or more memory efficiently
import shutil
shutil.copyfileobj(var2,file)