[英]Uploading and processing a file with PHP and Ajax
I would like to build a site that allows me to upload a file, and then show the progress of processing the file live. 我想建立一个允许我上传文件的站点,然后显示实时处理文件的进度。 The client would upload a delimited file. 客户端将上传定界文件。 Once uploaded I would verify the headers / columns in the data. 上传后,我将验证数据中的标题/列。 If this follows a given template then I would process the lines one at a time and would like to give live feed back to the user as this happens. 如果这遵循给定的模板,那么我将一次处理一行,并希望在发生这种情况时将实时反馈给用户。 I know PHP is ran at the time of the get request and thus I figure the approach would require ajax to accomplish my goals. 我知道在获取请求时就运行了PHP,因此我认为该方法将需要ajax来实现我的目标。 How do I go about doing something like this. 我该如何做这样的事情。 Any pseudo code, examples, or tutorials would be greatly appreciated. 任何伪代码,示例或教程将不胜感激。
I have done something similar in the past. 我过去做过类似的事情。 What you need to do it start the file processing. 您需要执行的操作开始文件处理。 After a set interval of time, such as a second, you can use an ajax call to poll the server to see the progress of processing the file. 在设置的时间间隔(例如一秒钟)之后,可以使用ajax调用来轮询服务器以查看处理文件的进度。
By the way, I would recommend jqueryUI progressbar for the progress bar and uploadify for the uploader. 顺便说一句,我会建议jQueryUI的进度的进度条和uploadify的上传。
Javascript : Javascript :
setTimeout("UpdateProgressBar();", 1000);
$.ajax({
type: "POST",
async: true,
data: // params go here
url: // path to web method
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
// Do stuff when file is finished processing
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (errorThrown != null)
alert(textStatus + " : " + errorThrown);
}
});
function UpdateProgressBar() {
$.ajax({
type: "POST",
async: true,
data: // my params go here
url: // path to web method
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
var totalRecords = result.d.TotalRecords;
var recordsProcessed = result.d.RecordsProcessed;
var percentProcessed = pagesProcessed / totalRecords;
$('#div_ProgressBar').progressbar("value", percentProcessed);
if (recordsProcessed < totalRecords)
setTimeout("UpdateProgressBar();", 800);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (errorThrown != null)
alert(textStatus + " : " + errorThrown);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.