使用django上傳檔案 主流有兩種方法 from表單以及ajax,為了自由度高一點,選擇了ajax來實現檔案的上傳
<div
class
="form-group"
>
<
label
for="exampleinputfile"
>file input
label
>
<
input
type
="file"
id="f"
>
<
p class
="help-block"
>example block-level help text here.
p>
div>
div>
<
div
class
="box-footer"
>
<
button
type
="submit"
class
="btn btn-primary"
id="fileupload"
>submit
button
>
div>
div>
需要注意的是 繫結了submit點選事件,點選了之後通過fromdata 方法 把檔案以字典 f : $("#f")[0].files[0]); 方式傳遞給前端 $("#f")[0]是把jq轉換為js物件
files[0]是獲取檔案,因為可能有很多檔案,這裡的意思是獲取第乙個檔案。
$("#fileupload").click(function ()})})
通過mession的值做不同的任務
defapi(request):
if request.method == "
post
"and request.post.get('
mession
') == "
fileupload":
f_obj = request.files.get("f"
)
(f_obj)
name =f_obj.name
(name)
destination = open(os.path.join("
/opt/upload
", name), '
wb+') #
開啟特定的檔案進行二進位制的寫操作
for chunk in f_obj.chunks(): #
分塊寫入檔案
上傳成功
")
在檔案傳輸的過程中,發現部分檔案無法傳輸,發現django對檔案的大小有限制,修改settings.py配置檔案
from django.core.files.uploadedfile importinmemoryuploadedfile
from django.core.files.uploadhandler import
temporaryfileuploadhandler
file_upload_handlers =[
'django.core.files.uploadhandler.memoryfileuploadhandler',
'django.core.files.uploadhandler.temporaryfileuploadhandler',
]file_upload_max_memory_size = 8621440data_upload_max_memory_size = 8621440data_upload_max_number_fields = 5000
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...