@action(detail=False, methods=['post'], url_path='upload')
def upload(self,request):
upload_obj = request.FILES.get('file')
# print(upload_obj) # 178bb3d5cfc06006d1d884951a20ff3.jpg
# print(upload_obj.name) # 178bb3d5cfc06006d1d884951a20ff3.jpg
date_path = datetime.now().strftime('%Y/%m/%d') # 2024/10/07
upload_path = os.path.join(settings.UPLOAD_PATH, date_path) # upload/2024/10/07
file_path = os.path.join(upload_path, upload_obj.name) # upload/2024/10/07\178bb3d5cfc06006d1d884951a20ff3.jpg
upload_url = default_storage.get_available_name(file_path) # upload/2024/10/07/178bb3d5cfc06006d1d884951a20ff3.jpg
save_path = default_storage.save(upload_url, upload_obj) # upload/2024/10/07/178bb3d5cfc06006d1d884951a20ff3.jpg
local_url = default_storage.url(save_path) # /media/upload/2024/10/07/178bb3d5cfc06006d1d884951a20ff3_felgHKw.jpg
abs_url = request.build_absolute_uri(local_url) # http://localhost:8000/media/upload/2024/10/07/178bb3d5cfc06006d1d884951a20ff3_felgHKw.jpg
print(local_url)
print(abs_url)
return Response("ok")
标签:07,url,合集,路径,upload,jpg,path,上传,178bb3d5cfc06006d1d884951a20ff3
From: https://www.cnblogs.com/cloud-2-jane/p/18450304