Сделал вывод процесса загрузски
This commit is contained in:
59
index.html
59
index.html
@@ -5,10 +5,25 @@
|
||||
<script src="lib_chunk_file.js"></script>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
|
||||
<style>
|
||||
li {
|
||||
padding: 10px 15px;
|
||||
list-style: none;
|
||||
border:solid 1px #ddd;
|
||||
border-radius: 5px;
|
||||
width: max-content;
|
||||
}
|
||||
|
||||
li span {
|
||||
display: inline-block;
|
||||
margin: 0 15px 0 0;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<input type="file" name="file_name">
|
||||
<input type="file" multiple name="file_name">
|
||||
|
||||
|
||||
<script>
|
||||
@@ -16,13 +31,43 @@
|
||||
|
||||
fileInput.addEventListener('change', (event) => {
|
||||
|
||||
statuss= document.createElement("ul");
|
||||
|
||||
event.target.after(statuss);
|
||||
|
||||
const files = event.target.files;
|
||||
const file = files[0]
|
||||
if (file) {
|
||||
uploadFileInChunks(file);
|
||||
}else{
|
||||
alert("no file")
|
||||
}
|
||||
|
||||
for (fil in files){
|
||||
file = files[fil]
|
||||
|
||||
|
||||
if (file.size) {
|
||||
|
||||
console.log(">>>>>>>>",fil)
|
||||
|
||||
li = document.createElement("li");
|
||||
|
||||
namefile = document.createElement("span");
|
||||
namefile.innerHTML =file.name;
|
||||
|
||||
chanh = document.createElement("b");
|
||||
chanh.id="status_bar_"+fil
|
||||
chanh.innerHTML = 0;
|
||||
|
||||
full = document.createElement("b");
|
||||
full.innerHTML = " / " + Number(file.size/1024/1024).toFixed(2);
|
||||
|
||||
li.append(namefile);
|
||||
li.append(chanh);
|
||||
li.append(full);
|
||||
|
||||
statuss.append(li);
|
||||
|
||||
|
||||
uploadFileInChunks(file,fil);
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
// Размер чанка в байтах
|
||||
const CHUNK_SIZE = 1024 * 1024; // 1MB
|
||||
|
||||
async function uploadFileInChunks(file) {
|
||||
async function uploadFileInChunks(file,num) {
|
||||
|
||||
const totalChunks = Math.ceil(file.size / CHUNK_SIZE);
|
||||
|
||||
|
||||
|
||||
for (let chunkIndex = 0; chunkIndex < totalChunks; chunkIndex++) {
|
||||
|
||||
@@ -15,7 +17,7 @@ async function uploadFileInChunks(file) {
|
||||
// Извлекаем чанк из файла
|
||||
const chunk = file.slice(start, end);
|
||||
|
||||
// Создаём FormData для отправки чанка
|
||||
// Создаём FormData для отправки чанкаd
|
||||
const formData = new FormData();
|
||||
formData.append('file_chunk', chunk);
|
||||
formData.append('chunk_index', chunkIndex);
|
||||
@@ -26,6 +28,12 @@ async function uploadFileInChunks(file) {
|
||||
await fetch('upload.php', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
}).
|
||||
then((res)=> res.json()).
|
||||
then((res)=>{
|
||||
|
||||
document.getElementById("status_bar_"+num).innerHTML = Number(res.size/1024/1024).toFixed(2) || 0
|
||||
console.log(res)
|
||||
});
|
||||
}
|
||||
}
|
||||
13
upload.php
13
upload.php
@@ -22,17 +22,24 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
fclose($in);
|
||||
}
|
||||
fclose($out);
|
||||
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
exit(json_encode(['status' => 'error', 'message' => 'Не удалось открыть временный файл']));
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
exit(json_encode(['status' => 'error', 'message' => 'Не удалось открыть временный файл', 'size'=>0]));
|
||||
}
|
||||
|
||||
// Если это последняя часть, переименовываем файл в итоговый
|
||||
if ($chunkIndex === $totalChunks - 1) {
|
||||
$finalFilePath = $uploadDir . $originalFilename;
|
||||
rename($tempFilePath, $finalFilePath);
|
||||
echo json_encode(['status' => 'success', 'message' => 'Файл успешно собран', 'path' => $finalFilePath]);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode(['status' => 'success', 'message' => 'Файл успешно собран', 'path' => $finalFilePath, 'size'=>filesize($finalFilePath)]);
|
||||
} else {
|
||||
echo json_encode(['status' => 'uploading', 'chunk' => $chunkIndex]);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode(['status' => 'uploading', 'chunk' => $chunkIndex, 'size' => filesize($tempFilePath)]);
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user