html5、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')

猜你喜欢

转载自blog.51cto.com/9429042/2119261