def getRemoteImage(request): result={} url=request.POST.get("upfile") result['state']=u'远程图片抓取成功' arr=url.split(ue_separate_ue) outSrc=[] for i,uri in enumerate(arr): outSrc.append('') if not isAllowFiles(uri,imgtype): result['state']=errorInfo['TYPE'] continue fileuri=urllib.urlopen(uri) if fileuri.getcode()==200 and fileuri.headers.type.find('image')!=-1: parser=ImageFile.Parser() for chunk in fileuri.readlines(): parser.feed(chunk) fileuri.close() size=int(fileuri.headers.dict['content-length']) img=parser.close() newfilename='' ufile=UeditorFile() try: newfilename=getSaveName(UPLOADDIR,uri.split('/')[-1]) ufile.filename=uri.split('/')[-1] ufile.realfilename=newfilename ufile.size=size ufile.title='' ufile.type='image' ufile.user=request.user ufile.save() img.save(UPLOADDIR+newfilename) except Exception,e: ufile.delete() result['state']=errorInfo['DIR'] continue else: # result['state']=errorInfo['SUCCESS'] outSrc.insert(i,newfilename) pass else: result['state']=u'图片地址错误' continue
def imageUp(request): result=getResult() f=request.FILES[fieldname] if not isAllowFiles(f.name,imgtype): result['state']=errorInfo['TYPE'] elif not isAllowSize(f.size,maxSize) : result['state']=errorInfo['SIZE'] else: parser=ImageFile.Parser() for chunk in f.chunks(): parser.feed(chunk) img=parser.close() newfilename='' ufile=UeditorFile() try: newfilename=getSaveName(UPLOADDIR,f.name) ufile.filename=f.name ufile.realfilename=newfilename ufile.size=f.size ufile.title=request.POST.get('pictitle','') ufile.type='image' ufile.user=request.user ufile.save() img.save(UPLOADDIR+newfilename) except Exception,e: ufile.delete() result['state']=errorInfo['DIR'] else:
def fileUp(request): session=Session.objects.get(pk=request.POST.get('sessionid','')) result=getResult() f=request.FILES[fieldname] if not isAllowFiles(f.name,filetype): # result['state']=errorInfo['TYPE'] result['state']=u'只允许上传 rar 和 zip 格式的数据' elif not isAllowSize(f.size,filemaxSize) : result['state']=u'最大允许上传1Mb的文件' else: newfilename='' ufile=UeditorFile() try: newfilename=getSaveName(UPLOADDIR,f.name) ufile.filename=f.name ufile.realfilename=newfilename ufile.size=f.size ufile.title=request.POST.get('pictitle','') ufile.type='att' ufile.user=User.objects.get(pk=session.get_decoded().get('_auth_user_id')) ufile.save() fileatt=open(UPLOADDIR+newfilename,'wb+') for chunk in f.chunks(): fileatt.write(chunk) fileatt.close() except Exception,e: ufile.delete() result['state']=errorInfo['DIR'] else: