views.py
#上傳檔案
defupload(request):
if request.method == "
post":
file_obj = request.files.get("
file")
with open(file_obj.name, "wb
") as f:
#chunks() 一點一點的上傳檔案
for chunk in
file_obj.chunks():
f.write(chunk)
return render(request, "
upload.html
")
模板
doctype html
>
<
html
lang
="zh-cn"
>
<
head
>
<
meta
charset
="utf-8"
>
<
title
>上傳檔案
title
>
head
>
<
body
>
<
form
action
="/upload/"
method
="post"
enctype
="multipart/form-data"
>
<
p>
<
input
type
="file"
name
="file"
>
p>
<
p>
<
input
type
="submit"
value
="上傳"
>
p>
form
>
body
>
html
>
總結:1、form表單
上傳檔案,一定要有
enctype="multipart/form-data"
2、檢視
獲取檔案物件
file_obj = request.files.get("file
")
獲取檔名稱
file_obj.name
內建的chunks()方法,按塊上傳檔案
for chunk infile_obj.chunks():f.write(chunk)
利用ajax上傳檔案
需要用到formdata儲存資料
並且要加
processdata: false,contenttype:
false,
django 檔案上傳
檔案上傳 當django處理上傳乙個檔案的時候,檔案資料被放在request.files中。這個文件解釋檔案怎麼樣被儲存在磁碟上或者記憶體中,怎樣定製預設的行為。基本檔案上傳 考慮乙個包含filefield的簡單的表單 from django import forms class uploadfil...
django 檔案上傳
檔案上傳 當django處理上傳乙個檔案的時候,檔案資料被放在request.files中。這個文件解釋檔案怎麼樣被儲存在磁碟上或者記憶體中,怎樣定製預設的行為。基本檔案上傳 考慮乙個包含filefield的簡單的表單 from django import forms class uploadfil...
Django檔案上傳
lang en charset utf 8 titletitle head action home method post enctype multipart form data type file name file type submit value 提交 p form div body htm...