Reputation: 794
i have an code, code is running well, problem with this code is that it did not get the filename when the file is uploaded.
views.py
@csrf_exempt
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
if 'filename' in request.FILES:
filename = request.FILES['filename']
else:
return HttpResponseRedirect('/user_profileform/')
else:
form = UploadFileForm()
return render_to_response('user_profile.html', {'form': form })
def handle_uploaded_file(f):
destination = open('media/name', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
my form is :-
<form action="/user_profileform/" method="POST" enctype="multipart/form-data" name="uform" id="userform">{% csrf_token %}
{{form}}
<input type="submit" value="submit" name="usubmit">
</form>
when using this the file is uploaded properly but did not get the file name,, why.
Upvotes: 1
Views: 108
Reputation: 29967
The issue is probably that you are trying to read the filename from the request.FILES
dictionary. This dictionary contains an UploadedFile
object for each file upload field in your form. The filename is a property of that UploadedFile
object. Try this:
if request.FILES['file'].name:
filename = request.FILES['file'].name
If that doesn't work, show us your actual form and the code where you are trying to use the filename.
Upvotes: 2