{# 载入js库 #} <script src='{% static "js/jquery-1.12.1.min.js" %}'></script> <script src='{% static "js/jquery.Huploadify.js" %}'></script> <body> <input type="text" value="100" id="t1"> <div id="upload"></div> <script> let name = $("#t1").val(); var up = $('#upload').Huploadify({ auto:false, fileTypeExts:'*.*', multi:true, fileSizeLimit:99999999999, showUploadedPercent:true, formData:{'name':'123'}, showUploadedSize:true, removeTimeout:9999999, method: 'post', uploader:'/md_admin/upload_img', onUploadStart:function(file){ console.log(file.name+'开始上传'); //$("#upload").Huploadify("settings","formData",{'name':'someValue'}); uploadify_option.formData={name:'0'}; }, onInit:function(obj){ console.log('初始化'); console.log(obj); }, onUploadComplete:function(file){ console.log(file.name+'上传完成'); }, onCancel:function(file){ console.log(file.name+'删除成功'); }, onClearQueue:function(queueItemCount){ console.log('有'+queueItemCount+'个文件被删除了'); }, onDestroy:function(){ console.log('destroyed!'); }, onSelect:function(file){ console.log(file.name+'加入上传队列'); }, onQueueComplete:function(queueData){ console.log('队列中的文件全部上传完成',queueData); } }); </script> </body>
#定义上传视图类 class UploadTest(View): #定义上传方法 def post(self,request): #接收文件,以对象的形式 img = request.FILES.get("file") print(request.POST.get('name','未收到参数')) #文件名称是name属性 #建立文件流对象 f = open(os.path.join(UPLOAD_ROOT,'',img.name),'wb') #写文件 遍历图片文件流 for chunk in img.chunks(): f.write(chunk) #关闭文件流 f.close() return HttpResponse(json.dumps({'status':'ok'},ensure_ascii=False),content_type='application/json')