我正在尝试使用html5的File API批量上传文件,然后在php的服务器端重新组装它。我正在上传视频,但是当我在服务器端合并文件时,文件大小增加了,并且变成了无效文件。请注意,以下html5代码仅适用于chrome浏览器。在Chrome 9中进行了测试,因为它使用了文件API的slice函数。有人可以指导我吗?谢谢
<?php $target_path = "uploads/"; $tmp_name = $_FILES['fileToUpload']['tmp_name']; $size = $_FILES['fileToUpload']['size']; $name = $_FILES['fileToUpload']['name']; $target_file = $target_path . basename($name); $complete = "complete.mov"; $com = fopen("uploads/".$complete, "ab"); error_log($target_path); // Open temp file $out = fopen($target_file, "wb"); if ( $out ) { // Read binary input stream and append it to temp file $in = fopen($tmp_name, "rb"); if ( $in ) { while ( $buff = fread( $in, 1048576 ) ) { fwrite($out, $buff); fwrite($com, $buff); } } fclose($in); fclose($out); } fclose($com); ?>
<!DOCTYPE html> <html> <head> <title>Upload Files using XMLHttpRequest</title> <script type="text/javascript"> window.BlobBuilder = window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder; function sendRequest() { var blob = document.getElementById('fileToUpload').files[0]; const BYTES_PER_CHUNK = 1048576; // 1MB chunk sizes. const SIZE = blob.size; var start = 0; var end = BYTES_PER_CHUNK; while( start < SIZE ) { var chunk = blob.slice(start, end); uploadFile(chunk); start = end; end = start + BYTES_PER_CHUNK; } } function fileSelected() { var file = document.getElementById('fileToUpload').files[0]; if (file) { var fileSize = 0; if (file.size > 1024 * 1024) fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB'; else fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB'; document.getElementById('fileName').innerHTML = 'Name: ' + file.name; document.getElementById('fileSize').innerHTML = 'Size: ' + fileSize; document.getElementById('fileType').innerHTML = 'Type: ' + file.type; } } function uploadFile(blobFile) { //var file = document.getElementById('fileToUpload').files[0]; var fd = new FormData(); fd.append("fileToUpload", blobFile); var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", uploadProgress, false); xhr.addEventListener("load", uploadComplete, false); xhr.addEventListener("error", uploadFailed, false); xhr.addEventListener("abort", uploadCanceled, false); xhr.open("POST", "upload.php"); xhr.onload = function(e) { alert("loaded!"); }; xhr.send(fd); //alert("oen over"); } function uploadProgress(evt) { if (evt.lengthComputable) { var percentComplete = Math.round(evt.loaded * 100 / evt.total); document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%'; } else { document.getElementById('progressNumber').innerHTML = 'unable to compute'; } } function uploadComplete(evt) { /* This event is raised when the server send back a response */ alert(evt.target.responseText); } function uploadFailed(evt) { alert("There was an error attempting to upload the file."); } function uploadCanceled(evt) { xhr.abort(); xhr = null; //alert("The upload has been canceled by the user or the browser dropped the connection."); } </script> </head> <body> <form id="form1" enctype="multipart/form-data" method="post" action="upload.php"> <div class="row"> <label for="fileToUpload">Select a File to Upload</label><br /> <input type="file" name="fileToUpload" id="fileToUpload" onchange="fileSelected();"/> <input type="button" value="cancel" onClick="uploadCanceled();"/> </div> <div id="fileName"></div> <div id="fileSize"></div> <div id="fileType"></div> <div class="row"> <input type="button" onclick="sendRequest();" value="Upload" /> </div> <div id="progressNumber"></div> </form> </body> </html>
Slice Function接受第二个参数作为length。当as mozSlice接受第二个参数作为结束