Why does Django file upload throws a IOError (2, 'No such file or directory') exception?
I'm writing a simple file uploader for a website. The user sees a form:
<form enctype="multipart/form-data" action="/uploader/" name="upload" method="post">
<input type="file" name="datafile" size="20">
{% csrf_token %}
<span class="whats_this">Maximum File Size is 100 MB</span>
<button class="button orange" type="start_upload" value="Submit" onclick="document.upload.submit() ">Start Upload</button>
<span class="cancel_button"><a href="#">Cancel</a></span>
</form>
and upon submit, I redirect to this function:
def uploadFiles(request):
if request.method == 'POST':
if(validate(request)):
file = request.FILES['datafile']
destination = open('upload开发者_运维问答edFiles/test.txt', 'wb+')
for chunk in file.chunks():
destination.write(chunk)
destination.close()
context = {'files': file.name}
return render_to_response('dashboard.html', context)
else:
...
When I try this functionality out, I'll always get an exception:
IOError at /uploader/
(2, 'No such file or directory')
uploader\views.py in uploadFiles, line 17
Line 17 is the line of the destination "destination = open('uploadedFiles/test.txt', 'wb+')". I've been trying this out quite a bit but it simply doesn't work. Am I missing something important here?
You don't say where you're trying to upload to, or where you've created the uploadedFiles
directory. The problem is that you're giving a relative path, but it's far from obvious where it is relative to - probably the manage.py
or .wsgi
file that's serving your site.
Give an absolute path, which for best results should be under MEDIA_ROOT
.
精彩评论