UPload 上传图片

HTML源代码:

 
    • {# 隐藏的图片提交路由#}
    • JS代码:

      
      
      
      

      控制器代码:upload.py

      #pupload上传图片
      from blog.utils import function
      import os.path
      def upload2(request):
          print("asdfasdfasdfaqwe")
          img_obj=request.FILES.get("file")#通过前台提交过来的图片资源   img_obj.name  avatar.jpg
          range_num=function.suijishu(15)#生成一个15位随机数
          print(range_num)
          # print(img_obj.name)
          img_type=os.path.splitext(img_obj.name)[1]#.jpg  获取文件名后缀
          new_img_path=os.path.join(settings.MEDIA_ROOT,"add_article_img",range_num+img_type)#E:\ftp\code\www\pro\media\add_article_img\676161546271228.jpg        #/media/add_article_img/123456.jpg
          img_type2 = img_type.replace(".", "")  #png
          # print(new_img_path)
          with open(new_img_path,"wb") as f:
              for line in img_obj:
                  f.write(line)
          response={
              "status":1,
              "message":"上传成功",
              'file':"/media/add_article_img/"+range_num+img_type,
              'file_type':img_type2
          }
          return HttpResponse(json.dumps(response))
      

      你可能感兴趣的:(Python---Django,Django,图片上传,plupload上传图片)