<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <!-- 2.创建页面元素 --> <div id="upload"> <input id="file" type="file" name="file" /> <button id="btnButton" type="button">提交2</button> </div> <script type="text/javascript" src="js/jquery-3.4.1.min.js"></script> <script type="text/javascript" src="js/jquery-form.js"></script> <script type="text/javascript"> $(function(){ $("#btnButton").click(function () { the sendRequest (); return to false ; }); }) var BYTES_PER_CHUNK = . 1 * 1024 * 1024 ; // for each slice file size as 1MB. var slices; var totalSlices; // send a request function the sendRequest () { var BLOB = document.getElementById ( " File " ) .files [ 0 ]; var Start = 0 ; var End; var index= 0; // 计算文件切片总数 slices = Math.ceil(blob.size / BYTES_PER_CHUNK); totalSlices= slices; while(start < blob.size) { end = start + BYTES_PER_CHUNK; if(end > blob.size) { end = blob.size; } uploadFile(blob, index, start, end); start = end; index++ ; IF (index > = totalSlices) LOCATION = " reboot.htm " ; } } // upload function the uploadFile (BLOB, index, Start, End) { var XHR; var FD; var the chunk; var sliceIndex = BLOB. name + index; the chunk = blob.slice (Start, End); // cutting file FD = new new the FormData (); fd.append ( " UpgradeFileName " , the chunk, sliceIndex); var XHR = new new the XMLHttpRequest (); xhr.open ( ' the POST ' , ' http://192.168.10.134:8080/Training/UploadVideo ' , to false ); // false refers to synchronous upload, because my small server memory, select synchronization, if the pursuit of speed, you can choose // ture, asynchronous upload xhr.send (fd); IF ((xhr.status > = 200 && xhr.status < 300 ) || xhr.status == 304 ) { the setTimeout ( "" , 10); }else{ uploadFile(blob, index, start, end); } } </script> </body> </html>
/// <Summary> /// file merge /// </ Summary> /// <param name = "Files"> Files </ param> /// <param name = "the fullName"> storage path </ param> public static void FileMerge (list < String > files, String the fullName) { IF (! (files.Count> 0 )) { the throw new new Exception ( " file list is empty " ); } foreach (string item in files) { if (!File.Exists(item)) { throw new Exception(string.Format("文件{0}不存在", item)); } } byte[] buffer = new byte[1024 * 100]; using (FileStream outStream = new FileStream(fullName, FileMode.Create)) { int readedLen = 0; FileStream srcStream = null; for (int i = 0; i < files.Count; i++) { srcStream = new FileStream(files[i], FileMode.Open); while ((readedLen = srcStream.Read(buffer, 0, buffer.Length)) > 0) { outStream.Write(buffer, 0, readedLen); } srcStream.Close(); } } }