search
Homephp教程PHP开发Jquery and BigFileUpload implement large file upload and progress bar display

Implementation method: Use the bigfileupload component of Gaoshanlaike. Use Gaoshanlaike's method to pop up a modal window, and then keep refreshing to obtain the progress. I always feel that the experience is not good, so I thought of using jquery to achieve non-refresh progress display. Because after submitting the page, it cannot be allowed to refresh the page, but the progress information returned by progress.aspx must be continuously obtained through ajax, so the ajaxform submission of jquery.form is used. After the ajaxform is submitted, if post-submission events are executed, such as inserting records into the database, it is still being debugged.

1. jquery’s progressbar, form, MultFile and related components are used

<script src="../../common/js/jquery-1.2.6.pack.js" type="text/javascript"></script> 
<script src="../../common/jqControl/packed/ui.core.packed.js" type="text/javascript"></script> 
<!--FORM--> 
<script src="../../common/jqControl/ajax/jquery.form.js" type="text/javascript"></script> 
<!--上传文件--> 
<script src="../../common/jqControl/upLoad/jquery.MultiFile.pack.js" type="text/javascript"></script> 
<!--进度条--> 
<script src="../../common/jqControl/packed/ui.progressbar.packed.js" type="text/javascript"></script> 
<!--对话框--> 
<!--我的JS --> 
<link href="../../common/Css/jquery-ui-themeroller.css" rel="stylesheet" type="text/css" /> 
<script src="../gadget/JS/uploadfile.js" type="text/javascript">

Jquery and BigFileUpload implement large file upload and progress bar display

2. The uploadfile.js code is as follows:

$(function() 
{ 
var info = &#39;<div style="display:none" id="uploadinfo">&#39;; 
info += &#39;<p>正在上传: <span id="uploadfilename"></span></p>&#39;; 
info += &#39;<p>上传速度: <span id="uploadrate"></span> </p>&#39;; 
info += &#39;<p>状态: <span id="uploadtitle"></span></p>&#39;; 
info += &#39;<p>预计剩余时间: <span id="uploadlefttime"></span></p>&#39;; 
info += &#39;<p>上传文件大小: <span id="uploadtotal"></span></p>&#39;; 
info += &#39;<p>已上传大小: <span id="uploadcurrent"></span></p>&#39;; 
info += &#39;<p><div id="uploadprogressbar"></div></p>&#39;; 
info += &#39;</div><div id="dialogsucc" > &#39;; 
$("body").append(info); 
//进度条 
$("#uploadprogressbar").progressbar(); 
//上传 
$(&#39;#fileupload&#39;).MultiFile(); 
$(&#39;#btshow&#39;).click(function() 
{ 
alert($("body").html()); 
}); 
//提交 
$(&#39;#btnsubmit&#39;).click(function() 
{ 
$("#uploadForm").ajaxSubmit( 
{ 
dataType: &#39;text&#39;, 
beforeSubmit: function(a,f,o) 
{ 
startProgress(); 
}, 
async:false, 
success: function(data) 
{ 
//$("#dialogsucc").text(data); 
//stopProgress(); 
if(data.succ==1) 
{ 
/* $("#dialogsucc").show(); 
$("#dialogsucc").dialog( 
{ 
modal: true, 
overlay: 
{ 
opacity: 0.5, 
background: "black" 
} 
}); */
} 
}, 
error:function(err) 
{ 
alert(err); 
} 
}); 
}); 
}); 
function getProgress(){ 
$.ajax({ 
type: "post", 
dataType:"json", 
url: "uploadProgress.aspx", 
data: "UploadID=" + $("#UploadID").val(), 
success: function(data){ 
$("#uploadprogressbar").progressbar("progress", data.percent); 
$("#uploadfilename").html(data.filename); 
$("#uploadrate").html(data.rate); 
$("#uploadtitle").html(data.info); 
$("#uploadlefttime").html(data.lefttime); 
$("#uploadtotal").html(data.total); 
$("#uploadcurrent").html(data.current); 
if(data.succ==-1){ 
alert(data.errmsg); 
} 
if (data.succ==0){ 
setTimeout("getProgress()", 1000); 
} 
if (data.succ==1){ 
return; 
} 
}, 
error:function(msg) 
{ 
alert(msg); 
} 
}); 
} 
function startProgress(){ 
$("#uploadinfo").show(); 
setTimeout("getProgress()", 500); 
} 
function stopProgress() 
{ 
$("#uploadinfo").hide(); 
}

3: The code of progress.aspx is as follows:

protected void Page_Load(object sender, EventArgs e) 
{ 
try
{ 
string s = "{"; 
UploadContext upload = UploadContextFactory.GetUploadContext(Request["UploadID"]); 
//初始化 
if (upload.Status == uploadStatus.Initializing) 
{ 
s += responeJSON(upload, "0", ""); 
} 
if (upload.Status == uploadStatus.Uploading) 
{ 
s += responeJSON(upload, "0", ""); 
} 
if (upload.Status == uploadStatus.Complete) 
{ 
s += responeJSON(upload, "1", ""); 
} 
if (upload.Status == uploadStatus.HasError) 
{ 
s += responeJSON(upload, "-1", ""); 
} 
s += "}"; 
Response.Write(s); 
} 
catch (Exception err) 
{ 
//throw err; 
Response.Write("{&#39;info&#39;:&#39;" + err.Message.Replace("&#39;", "") + "&#39;,&#39;succ&#39;:-1}"); 
} 
} 
private string responeJSON(UploadContext upload, string succ,string errmsg) 
{ 
string s = ""; 
s += "&#39;info&#39;:&#39;" + upload.FormatStatus + "&#39;" ; 
s += ",&#39;percent&#39;:&#39;" + Convert.ToInt32((upload.Readedlength * 100.0 / upload.TotalLength)).ToString() + "&#39;"; 
s += ",&#39;current&#39;:&#39;" + (upload.Readedlength/1024).ToString() + "KB&#39;"; 
s += ",&#39;total&#39;:&#39;" + (upload.TotalLength/1024).ToString() + "KB&#39;"; 
s += ",&#39;rate&#39;:&#39;" + upload.FormatRatio + "&#39;"; 
s += ",&#39;filename&#39;:&#39;" + upload.CurrentFile + "&#39;"; 
s += ",&#39;cancel_upload&#39;:" + 0 ; 
s += ",&#39;lefttime&#39;:&#39;" + upload.LeftTime + "&#39;"; 
s += ",&#39;succ&#39;:" + succ; 
s += ",&#39;errmsg&#39;:&#39;" + errmsg +"&#39;"; 
return s; 
}

4. After the ajaxForm is executed, it can run normally. How can I perform other operations after uploading the file? I will post it after researching it

As mentioned above The editor introduced to you Jquery and BigFileUpload to implement large file upload and progress bar display. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to Jquery and BigFileUpload implementing large file upload and progress bar display, please pay attention to 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment