Working example - just configure your settings (MEDIA_ROOT) and add
FileField to your model and you're done.
# TEMPLATE:
<form action="{{myapp}}/upload/" method="post"
enctype="multipart/form-data" >
File: <input type="file" name="file" id="file">
<input type="submit" value="Upload File">
</form>
# VIEW:
def save_file( request ):
import os.path
# where to store files. Probably best defined in settings.py
filepath = '/home/simon/files/'
# right, so 'file' is the name of the file upload field
filename = request.FILES[ 'file' ][ 'filename' ]
filetype = request.FILES[ 'file' ][ 'content-type' ]
#the uploaded data from the file
data = request.FILES[ 'file' ][ 'content' ]
# the full file path and name
fullfilepath = os.path.join( filepath, filename )
# clean up filenames & paths:
fullfilepath = os.path.normpath( fullfilepath )
fullfilepath = os.path.normcase( fullfilepath )
# try to write file to the dir.
try:
f = open( fullfilepath, 'wb' ) # Writing in binary mode for windows..?
f.write( data )
f.close( )
except:
# something went wrong
--Simon
############
def UploadFile(request):
if request.POST:
print "We are in POST"
import os.path
filepath = '/Media/Static/'
filename = request.FILES[ 'file' ][ 'filename' ]
filetype = request.FILES[ 'file' ][ 'content-type' ]
data = request.FILES[ 'file' ][ 'content' ]
fullfilepath = os.path.join( filepath, filename )
fullfilepath = os.path.normpath( fullfilepath )
fullfilepath = os.path.normcase( fullfilepath )
f = open( fullfilepath, 'wb' )
f.write( data )
f.close( )
print "SUCCESS"
return HttpResponse("SUCCESS")
else:
print "We are in GET "
errors = new_data = {}
manipulator = users.AddManipulator()
form = formfields.FormWrapper(manipulator, new_data, errors)
t = template_loader.get_template("board/UploadFile")
c = Context(request, {'form': form})
return HttpResponse(t.render(c))
#################
and the form like this:
#############
<form action="." method="post" enctype="multipart/form-data">
<p>Choose a file to be loaded: <input type="file" name="file"
id="file"> <input
type="submit" value="Upload File"> </p>
</form>
#############
but now the problem is that the program will never go to request.POST
section.
But when I remove enctype="multipart/form-data" from the form, the
program will go to request.POST section but I will receive the error:
##############
Request Method: POST
Request URL: http://localhost:8082/Up/
Exception Type: MultiValueDictKeyError
Exception Value: "Key 'file' not found in MultiValueDict {}"
Exception Location:
C:\PYTHON23\lib\site-packages\django\utils\datastructures.py in
__getitem__, line 73
#######
and the problem is with
filename = request.FILES[ 'file' ][ 'filename' ] ...
But why? Thank you for the reply
Regards,
L.
###########
<form action="./" method="post" enctype="multipart/form-data">
<p>Choose a file n to be loaded: <input type="file" name="file"
id="file"> <input
type="submit" value="Upload File"> </p>
</form>
##########
but the result is the SAME.
The program will never go through the request.POST
section of the view. But why??
Any help would be appreciated.
Thank you
L.