def put(request): file = request.FILES.get('image', None) #读取二进制内容 content = [] if file: for chunk in file.chunks(): content.append(chunk) #获取存储图片的目录和文件信息 file_name = Image.__get_file_name(file.name) store_dir = time.strftime('%Y%m%d') dir_path = os.path.join(settings.UPLOAD_DIR, store_dir) if not os.path.exists(dir_path): os.makedirs(dir_path) file_path = os.path.join(dir_path, file_name) #写图片文件内容 dst_file = open(file_path, 'wb') print >> dst_file, ''.join(content) dst_file.close() #保存图片信息到mysql中 image_path = '/static/upload/%s/%s' % (store_dir, file_name) try: value = upload_image_to_upyun(file_path, image_path) image_path = value except: image_path = PANDA_HOST + '/static/upload/%s/%s' % (store_dir, file_name) image = models.Image.objects.create(user=request.user, path=image_path) response = create_response(200) response.data = {'id': image.id, 'path': image_path} return response.get_response()