django ajax上传文件

文件上传

https://www.bbsmax.com/A/l1dyQexdem/


 



处理图片:

def pic_class(request):
    upload_file = request.FILES.get("myfile", None)  # 获取上传的文件,如果没有文件,则默认为None
    file_obj = request.FILES.get('file')
    if file_obj:   # 处理附件上传到方法
        request_set = {}
        print('file--obj', file_obj)
        # user_home_dir = "upload/%s" % (request.user.userprofile.id)
        # accessory_dir = settings.accessory_dir
        # if not os.path.isdir(accessory_dir):
        #     os.mkdir(accessory_dir)
        scr = Image.open(file_obj)
        img= np.asarray(scr)
 
  
with open(upload_file, 'wb') as new_file:
    for chunk in file_obj.chunks():
        new_file.write(chunk)


### get the inmemory file
data = request.FILES.get('btn_file') # get the file from the curl
### write the data to a temp file
tup = tempfile.mkstemp() # make a tmp file
f = os.fdopen(tup[0], 'w') # open the tmp file for writing
f.write(data.read()) # write the tmp file
f.close()
### return the path of the file
filepath = tup[1] # get the filepath
return filepath


你可能感兴趣的:(Django)