59 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Python
		
	
	
	
| from rest_framework.response import Response
 | |
| from rest_framework.views import APIView
 | |
| from rest_framework.parsers import MultiPartParser
 | |
| from rest_framework.permissions import IsAuthenticated
 | |
| from PIL import Image
 | |
| from django.conf import settings
 | |
| 
 | |
| from datetime import datetime
 | |
| import os
 | |
| import uuid
 | |
| 
 | |
| class UploadFileView(APIView):
 | |
|     permission_classes = [IsAuthenticated]
 | |
|     parser_classes = (MultiPartParser,)
 | |
|     def post(self, request, *args, **kwargs):
 | |
|         fileobj = request.FILES['file']
 | |
|         file_name = fileobj.name.encode('utf-8').decode('utf-8')
 | |
|         file_name_new = str(uuid.uuid1()) + '.' + file_name.split('.')[-1]
 | |
|         subfolder = os.path.join('media', datetime.now().strftime("%Y%m%d"))
 | |
|         if not os.path.exists(subfolder):
 | |
|             os.mkdir(subfolder)
 | |
|         file_path = os.path.join(subfolder, file_name_new)
 | |
|         file_path = file_path.replace('\\', '/')
 | |
|         with open(file_path, 'wb') as f:
 | |
|             for chunk in fileobj.chunks():
 | |
|                 f.write(chunk)
 | |
|         resdata = {"name": file_name, "path": '/' + file_path}
 | |
|         return Response(resdata)
 | |
| 
 | |
| # def uploadfile(fileObj ,file_type='pic'):
 | |
| #     """ 一个公用的上传文件的处理 """
 | |
| #     resp = ''
 | |
| #     if fileObj:
 | |
| #         filename = fileObj.name.decode('utf-8', 'ignore')
 | |
| #         fileExt = filename.split('.')[1]
 | |
| #         file_name = str(uuid.uuid1())
 | |
| #         subfolder = time.strftime("%Y%m")
 | |
| #         if not os.path.exists(settings.MEDIA_ROOT[0] + subfolder):
 | |
| #             os.makedirs(settings.MEDIA_ROOT[0] + subfolder)
 | |
| #         path = str(subfolder + '/' + file_name + '.' + fileExt)
 | |
| 
 | |
| #         if fileExt.lower() in ('jpg', 'jpeg', 'bmp', 'gif', 'png', "rar", "doc", "docx", "zip", "pdf", "txt", "swf", "wmv"):
 | |
| 
 | |
| #             phisypath = settings.MEDIA_ROOT[0] + path
 | |
| #             destination = open(phisypath, 'wb+')
 | |
| #             for chunk in fileObj.chunks():
 | |
| #                 destination.write(chunk)
 | |
| #             destination.close()
 | |
| 
 | |
| #             if file_type == 'pic':
 | |
| #                 if fileExt.lower() in ('jpg', 'jpeg', 'bmp', 'gif', 'png'):
 | |
| #                     im = Image.open(phisypath)
 | |
| #                     im.thumbnail((720, 720))
 | |
| #                     im.save(phisypath)
 | |
| 
 | |
| #             real_url = '/static/upload/' + subfolder + '/' + file_name + '.' + fileExt
 | |
| #             resp = "{'original':'%s','url':'%s','title':'%s','state':'%s'}" % (
 | |
| #             filename, real_url, 'source_file_tile', 'SUCCESS')
 | |
| #     return resp |