yuncaige

1、前端基础

  1.form表单上传文件必须使用post方式,设置method="post"

  2.form表单上传文件时,需要设置enctype="multipart/form-data"

2、form表单设置

<!--使用bootstrap样式-->
<form class="form-horizontal" method="post" action="/commodityAdd/" enctype="multipart/form-data">
    <div class="form-group">
        <label for="inputImage" class="col-sm-2 control-label">标志图片</label>
        <div class="col-sm-10">
            <input type="file" name="commodityImage" id="inputImage">
            <p class="help-block">请传入800X800图片.</p>
        </div>
    </div>
    <div class="form-group">    
        <button type="submit" class="btn btn-danger">提 交</button>
    </div>
</form>

3、Django后台接收文件

  1.request.FILES ---->存放所有文件的大字典

  2.request.FILES.get("commodityImage",None) ---->获取form表单中name="commodityImage"的文件输入框中上传的文件

def commodityAdd(request):
   #获取图片,此时该文件格式为二进制 commodity_image
= request.FILES.get("commodityImage", None)
  #使用二进制的方式打开新建文件,不改变文件,直接写入 with open("picture.jpg",mode="wb") as image_file: for content in commodity_image: image_file.write(content) return render(request, "commodityAdd.html")

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-04-20
  • 2021-11-02
  • 2021-12-07
  • 2021-05-11
  • 2022-12-23
  • 2021-09-22
  • 2022-01-01
猜你喜欢
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-02-23
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案