首页 > Web开发 > 详细

文件上传

时间:2019-12-08 21:07:19      阅读:91      评论:0      收藏:0      [点我收藏+]
技术分享图片
# ***********文件上传*************
def fileupload(request):
    if request.method==GET:
        return render(request,fileupload.html)
    if request.method==POST:
        # FILES
        print(request.FILES)
        print(type(request.FILES.get(myfile)))
        # 从字典里根据名字,把文件取出来
        myfile=request.FILES.get(myfile)
        from django.core.files.uploadedfile import InMemoryUploadedFile
        # 文件名字
        name=myfile.name
        # 打开文件,把上传过来的文件存到本地
        with open(name,wb) as f:
            # for line in myfile.chunks():
            for line in myfile:
                f.write(line)
        return HttpResponse(ok)
视图函数
技术分享图片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
{#<form action="" method="post" enctype="application/x-www-form-urlencoded">#}
    <input type="file" name="myfile">
    <input type="text" name="password">
    <input type="submit" value="提交">
</form>

</body>
</html>
前端页面

 

文件上传

原文:https://www.cnblogs.com/xuqidong/p/12007212.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!