from django.http import HttpResponse from django.middleware.csrf import get_token from django.shortcuts import render from django.views.decorators.csrf import csrf_exempt from django.views.decorators.http import require_http_methods
@require_http_methods(["GET", "POST"]) defindex(request): response = {} try: if request.method == 'GET': a = get_token(request) # print(a) if request.method == 'POST': req = request.FILES.get('file') print(req) print(req.size) # for chunk in req.chunks(): # print(chunk) content = [] # 逐行读取文件内容, 并且消除换行 for line in req.read().splitlines(): print(line) # 将每行内容添加到list中 content.append(line) destination = open( os.path.join("f://vue", req.name), 'wb+') # 打开特定的文件进行二进制的写操作 for chunk in req.chunks(): # 分块写入文件 destination.write(chunk) destination.close() print(content[2]) response['code'] = 200 except Exception as e: response['code'] = 1 return HttpResponse(response)