Large File Upload Service
First, the front-end
[Webuploader] (http://fex.baidu.com/webuploader/"Webuploader")
Second, back end
Django 2.0.0
Only the core code is posted here:
Front-end:
<! DOCTYPE html>
Back end of:
Routing
path(‘files/upload/‘, views.fileupload,name=‘图片分片上传‘), path(‘upload/complete/‘, views.fileMerge,name=‘上传成功合并‘),
View:
@csrf_exemptdef FileUpload (Request): if Request.method = = ' POST ': upload_file = Request. Files.get (' file ') task = Request. Post.get (' task_id ') # Gets the file unique identifier chunk = Request. Post.get (' chunk ', 0) # Gets the sequence number of the Shard in all shards filename = '%s%s '% (task, chunk) # constitutes the Shard unique identifier print ("Filename=", fil ename) Default_storage.save ('./upload/%s '% Filename,contentfile (Upload_file.read ())) # Save Shards to local return render_t O_response (' upload.html ', Locals ()) @csrf_exemptdef 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 E LSE '.%s '% ext # build file suffix name chunk = 0 with open ('./upload/%s%s '% (task, ext), ' WB ') as Target_file: # Create new file While True:try:filename = './upload/%s%d '% (task, chunk) source_file = open (fi Lename, ' RB ') # Opens each shard Target_file.write (Source_file.read ()) # reads the Shard content to write a new file source_file.cl OSE () except ioerror:break chunk + = 1 os.remove (filename) # Delete the Shard and save space Return Render_to_response (' upload.html ', locals ())
:
Django+python Large File Upload