0
我有這段代碼,代碼運行良好,但問題是上傳文件時沒有得到文件名。我的代碼 -文件名在django上傳
views.py
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:
raise Exception('did not get any name')
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()
形式是: -
<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>
的錯誤是: -
沒有得到任何名稱
你有一個'file'或'filename'可以訪問?這裏'handle_uploaded_file(request.FILES ['file'])'你使用'file',並且進一步使用'filename' –
發表你的表單代碼,以便很容易找出問題。 – arulmr