我尝试使用此代码保存图像。它将图像保存在给定路径,但不获取文件名。我怎么才能得到它?
@csrf_exempt
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if 'filename' in request.FILES:
filename = request.FILES['filename'].name
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
return HttpResponseRedirect('/user_profileform/')
else:
form = UploadFileForm()
return render_to_response('user_profile.html', {'form': form })
def handle_uploaded_file(f):
destination = open('media/filename', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()