标签:submit rom .post obj open image filename 分块 png
上接简单的 Django 项目 https://www.cnblogs.com/klvchen/p/10155538.html
这里需要注意两个地方:
post
enctype="multipart/form-data"
在 index.html 加入input 标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello worlds</h1>
<form action="/klvchen/" method="post" enctype="multipart/form-data">
<p><input type="file" name="upload"></p>
<p><input type="submit" value="submit"></p>
</form>
</body>
</html>
修改 views.py
from django.shortcuts import render
def klvchen(req):
print("前端数据: ", req.POST)
print("file:", req.FILES)
for item in req.FILES:
obj = req.FILES.get(item) # 获取要写入的文件
filename = obj.name # 获取文件名
f = open(filename, ‘wb‘)
for line in obj.chunks(): # 分块写入
f.write(line)
f.close()
return render(req, "index.html")
成功上传文件
标签:submit rom .post obj open image filename 分块 png
原文地址:https://www.cnblogs.com/klvchen/p/10168174.html