Home>Article>Backend Development> How to implement download progress bar in php

How to implement download progress bar in php

藏色散人
藏色散人 Original
2021-10-28 10:31:40 2781browse

php method to implement download progress bar: 1. Create the "download.php" file with code such as "switch ($action) {case 'prepare-download'...}"; 2. Create js The code displays a progress bar.

How to implement download progress bar in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How to implement the download progress bar in php?

PHP remote file download progress bar implementation

download.php

getMessage()); } return json(compact('tmp_path')); break; case 'get-file-size': // 这里检测下 tmp_path 是否存在 if (file_exists($tmp_path)) { // 返回 JSON 格式的响应 return json(['size' => filesize($tmp_path)]); } break; default: # code... break; }

js

// 咋触发这个函数我就不举例了 function downloadFile() { var file_size = 0; var progress = 0; console.log("Prepared to download"); $.ajax({ url: './download.php?action=prepare-download', type: 'GET', dataType: 'json', beforeSend: function() { $('#update-button').html(' 正在准备').prop('disabled', 'disabled'); }, }) .done(function(json) { console.log(json); file_size = json.file_size; $('#file-size').html(file_size); // 显示进度条 console.log("started downloading"); $.ajax({ url: './download.php?action=start-download', type: 'POST', dataType: 'json' }) .done(function(json) { // set progress to 100 when got the response progress = 100; console.log("Downloading finished"); console.log(json); }) .fail(showAjaxError); var interval_id = window.setInterval(function() { $('#imported-progress').html(progress); $('.progress-bar').css('width', progress+'%').attr('aria-valuenow', progress); if (progress == 100) { clearInterval(interval_id); // 到此远程文件下载完成,继续其他逻辑 } else { $.ajax({ url: './download.php?action=get-file-size', type: 'GET' }) .done(function(json) { progress = (json.size / file_size * 100).toFixed(2); updateProgress(progress); console.log("Progress: "+progress); }) .fail(showAjaxError); } }, 300); }) .fail(showAjaxError); }

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement download progress bar in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn