Using HTML front end of the XMLHttpRequest () and the FormData () segment upload large files

 

Web page for uploading large files to the backend

 

### 前端代码
<
body> <input type="file" name="video" id="file" /> <input type="button" class="btn btn-info" id="sub" value="提交"> <script> $("#sub").on("click",selfile ()) # trigger uploaded when the submit button is clicked //;10*1024*1024= const LENGTHselfile () {function The size of each upload var File = document.getElementsByName ( ' Video ' ) [ 0 ] .files [ 0 ]; // file object var filename = document.getElementsByName ( ' Video ' ) [ 0 ] .files [ 0 ] .name ; var totalSize = file.size; // total file size var start = 0 ; // for each upload starting byte var End = start + the LENGTH; //End of each upload byte var FD = null ; // form data object creates var BLOB = null ; // binary object var XHR = null ; // XHR objects the while (Start < totalSize) { FD = new new the FormData (); // each need to recreate XHR = new new the XMLHttpRequest (); // need to create and set each parameter xhr.open ( ' the POST ' , ' / QQQ / ' , to false); BLOB = file.slice (Start, End); // The length of the data to be uploaded each time taken fd.append ( ' file_data ' , BLOB); // add the data to the object fd fd.append ( ' filename ' , filename); // Gets the name of the file fd.append ( " FILE_SIZE " , totalSize); xhr.send (fd); // to upload the data fd // re-set the start and end of start = end; end = start + LENGTH ; } } </ Script> </body>

 

##python后端路由视图函数代码:

def qqq(request):
        file_path = "c:aa.txt"
        with open(file_path,"ab") as f:
            for line in request.FILES.get("file_data"):
                f.write(line)

 

Welcome to hug ~ ~ ~

 

Guess you like

Origin www.cnblogs.com/dongxixi/p/11007346.html