Django+python Large File Upload

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.