Created
December 29, 2011 15:05
-
Star
(106)
You must be signed in to star a gist -
Fork
(27)
You must be signed in to fork a gist
-
-
Save shiawuen/1534477 to your computer and use it in GitHub Desktop.
Sample to upload file by chunk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html lang="en-US"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>test upload by chunk</title> | |
</head> | |
<body> | |
<input type="file" id="f" /> | |
<script src="script.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var f = document.getElementById('f'); | |
if (f.files.length) | |
processFile(); | |
f.addEventListener('change', processFile, false); | |
function processFile(e) { | |
var file = f.files[0]; | |
var size = file.size; | |
var sliceSize = 256; | |
var start = 0; | |
setTimeout(loop, 1); | |
function loop() { | |
var end = start + sliceSize; | |
if (size - end < 0) { | |
end = size; | |
} | |
var s = slice(file, start, end); | |
send(s, start, end); | |
if (end < size) { | |
start += sliceSize; | |
setTimeout(loop, 1); | |
} | |
} | |
} | |
function send(piece, start, end) { | |
var formdata = new FormData(); | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', '/upload', true); | |
formdata.append('start', start); | |
formdata.append('end', end); | |
formdata.append('file', piece); | |
xhr.send(formdata); | |
} | |
/** | |
* Formalize file.slice | |
*/ | |
function slice(file, start, end) { | |
var slice = file.mozSlice ? file.mozSlice : | |
file.webkitSlice ? file.webkitSlice : | |
file.slice ? file.slice : noop; | |
return slice.bind(file)(start, end); | |
} | |
function noop() { | |
} | |
})(); |
Hello,
I have reviewed your work, thank you, but a more practical solution is needed.
index.html
js.js
upload.php
Consisting of only 3 files like,
chunk-size value can be adjusted,
Working with XMLHttpRequest,
Can you suggest a workable code that can send 7GB files without additional things such as composer?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I open a new repo to share my solution: https://github.com/woodchucker/SplitMergePHP.git