通过配置静态文件夹的形式上传:
view视图代码:
def upload(request):
if request.method == "POST":
obj = request.FILES.get("test")
import os
f = open(os.path.join(settings.MEDIA_ROOT, 'upload', obj.name), 'wb')
for chunk in obj.chunks():
f.write(chunk)
f.close()
result = {'result': 'ok', 'filename': obj.name, 'media_root': os.path.join(settings.MEDIA_ROOT, 'upload')}
return HttpResponse(json.dumps(result))
settings配置:
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] # 存放静态文件的目录,其中也可以包含url
# 文件上传
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls代码:
path('u