django+python大文件上传-创新互联
大文件上传服务
一、前端
[webuploader](http://fex.baidu.com/webuploader/ ''webuploader'')
二、后端
django 2.0.0
这里只贴出核心的代码:
前端的:
Title
选择文件
后端的:
路由
path('files/upload/', views.fileupload,name='图片分片上传'),
path('upload/complete/', views.fileMerge,name='上传成功合并'),
视图:
@csrf_exempt
def fileupload(request):
if request.method == 'POST':
upload_file = request.FILES.get('file')
task = request.POST.get('task_id') # 获取文件唯一标识符
chunk = request.POST.get('chunk', 0) # 获取该分片在所有分片中的序号
filename = '%s%s' % (task, chunk) # 构成该分片唯一标识符
print("filename=",filename)
default_storage.save('./upload/%s' % filename,ContentFile(upload_file.read())) # 保存分片到本地
return render_to_response('upload.html',locals())
@csrf_exempt
def fileMerge(request):
print(request.GET)
task = request.GET.get('task_id')
ext = request.GET.get('filename', '')
upload_type = request.GET.get('type')
if len(ext) == 0 and upload_type:
ext = upload_type.split('/')[1]
ext = '' if len(ext) == 0 else '.%s' % ext # 构建文件后缀名
chunk = 0
with open('./upload/%s%s' % (task, ext), 'wb') as target_file: # 创建新文件
while True:
try:
filename = './upload/%s%d' % (task, chunk)
source_file = open(filename, 'rb') # 按序打开每个分片
target_file.write(source_file.read()) # 读取分片内容写入新文件
source_file.close()
except IOError:
break
chunk += 1
os.remove(filename) # 删除该分片,节约空间
return render_to_response('upload.html',locals())
效果图:
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
当前题目:django+python大文件上传-创新互联
新闻来源:http://azwzsj.com/article/cdgsgg.html