1. 程式人生 > >html5、django上傳文件

html5、django上傳文件

http html django 上傳文件

前端代碼:

<!DOCTYPE html>
<html>
<head>
<title>uploadFile</title>

</head>
<body>

    <form method="POST" action="/upload"  enctype="multipart/form-data">
        <input type="file" name="myFile">
        <input type="submit" value="提交" />
    </form>

</body>

</html>

後端代碼:
def upload(request):
##request.FILES 為類字典類型,健為前段name指定的值,字典值為UploadFile對象,前端form中需要指定enctype="multipart/form-data"
##<input type="file" name="myFile">
uploadFileObj = request.FILES[‘myFile‘]
uploadFileName = uploadFileObj.name
uploadFileSize = uploadFileObj.size

##上傳文件
with open(uploadFileName,"wb") as saveFile:
    for chunk in uploadFileObj.chunks():
        saveFile.write(chunk)

return HttpResponse(‘file upload OK‘)

html5、django上傳文件