VUE+fcup.js+Python,前端分片后依次调用分片文件上传接口,分片上传回调接口
前端页面展示:
前端实现就不贴啦,自行百度~
后端代码实现:
分片文件上传接口:
@login_required
def upload_action(request):
if request.method == 'POST': # 获取对象
# print(param1)
# obj = request.FILES.getlist('fafafa')
CurDir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
file = request.FILES['file_data'] #获取文件Base64
target_filename = request.POST.get('file_name') # 获取上传文件的文件名
fileName = target_filename.split(".")[0] # 获取上传文件的文件名不包含后缀
chunk = request.POST.get('file_index') # 获取该分片在所有分片中的序号
file_suffix = request.POST.get('file_suffix') # 获取该分片在所有分片中的序号
file_total = request.POST.get('file_total') # 获取该分片总数
param1 = request.POST.get('param1') # 获取上传类型
# 根据出过来的参数不同区分保存路径
if param1 == '1' or param1 == '1/':
# 1代表上传升级包
print('升级包上传')
folder = CurDir + os.sep + "script" + os.sep + "shell"
elif param1 == '2' or param1 == '2/':
# 2代表上传升级脚本
print('升级脚本上传')
folder = CurDir + os.sep + "script" + os.sep + "shell" + os.sep + "bash_dir"
else:
data1 = {
"code": 999,
"msg": 'Error,just upload tgz or bash! %s' % param1
}
return HttpResponse(json.dumps(data1))
# 清除目录下所有文件
if chunk == '1':
num = 1 # 分片序号
while int(num) <= int(file_total):
filename = folder + os.sep + '%s%s.%s' % (fileName, num, file_suffix)
if os.path.exists(filename):
os.remove(filename) # 删除该分片
num += 1
filename = folder + os.sep + '%s%s.%s' % (fileName, chunk, file_suffix)
fs = FileSystemStorage()
fs.save(filename, file)
data1 = {
"code": 200,
"msg": '升级文件分片 %s 已成功上传!' % chunk
}
return HttpResponse(json.dumps(data1))
else:
return render(request, 'task/upload.html', {'upload_type': param1})
@login_required
def upload_success(request): # 所有分片均上传完后被调用
target_filename = request.POST.get('file_name') # 获取上传文件的文件名
fileName = target_filename.split(".")[0] # 获取上传文件的文件名不包含后缀
file_suffix = target_filename.split(".")[1] # 获取上传文件的后缀
file_md5 = request.POST.get('file_md5') # 获取文件的md5
file_total = request.POST.get('file_total') # 获取该分片总数
param1 = request.POST.get('param1') # 获取上传类型
print('上传文件MD5:' + file_md5)
CurDir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if param1 == '1' or param1 == '1/':
# 1代表上传升级包
print('升级包上传')
folder = CurDir + os.sep + "script" + os.sep + "shell"
elif param1 == '2' or param1 == '2/':
# 2代表上传升级脚本
print('升级脚本上传')
folder = CurDir + os.sep + "script" + os.sep + "shell" + os.sep + "bash_dir"
else:
data1 = {
"code": 999,
"msg": 'Error,just upload tgz or bash! %s' % param1
}
return HttpResponse(json.dumps(data1))
chunk = 1 # 分片序号
with open(folder + os.sep + target_filename,
'wb') as target_file: # 创建新文件
while int(chunk) <= int(file_total):
try:
filename = folder + os.sep + '%s%s.%s' % (fileName, chunk, file_suffix)
source_file = open(filename, 'rb') # 按序打开每个分片
target_file.write(source_file.read()) # 读取分片内容写入新文件
source_file.close()
except IOError:
break
chunk += 1
os.remove(filename) # 删除该分片,节约空间
# 获取文件的md5
d5 = hashlib.md5()
with open(r"" + folder + os.sep + '%s' % target_filename,
'rb') as f:
while True:
data = f.read(2024)
if not data:
break
d5.update(data) # update添加时会进行计算
print('组装后的文件MD5:' + d5.hexdigest())
if file_md5 != d5.hexdigest():
data = {
"code": 999,
"msg": '%s 升级文件上传失败,MD5值不一致!' % target_filename
}
return HttpResponse(json.dumps(data))
data1 = {
"code": 200,
"msg": '%s 升级文件已成功上传!' % target_filename
}
return HttpResponse(json.dumps(data1))
以上~别忘了一键三连呀(✪ω✪)