表单上传文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="ming" method="post" enctype="multipart/form-data">
    <p><input type="file" name="file"></p>
    <p><input type="submit" value="提交"></p>
</form>
</body>
</html>

关键参数enctype="multipart/form-data"

from django.shortcuts import render

# Create your views here.

def ming(request):
    if request.method == "POST":
        file = request.FILES.get("file")
        with open(file.name,'wb') as f:
            for line in file:
                f.write(line)
    return render(request,'ming.html')

从那么get到数据,逐行写入

猜你喜欢

转载自blog.csdn.net/qq_34788903/article/details/88382425