<form id="form1" runat="server"> </div> <ext:ResourceManager ID="run1" runat="server"> </ext:ResourceManager> <ext:Panel ID="pan1" runat="server"> <Items> <ext:FileUploadField ID="fl1" Width="300" Icon="FilmAdd" runat="server"> </ext:FileUploadField> <ext:Button runat="server" ID="btnSub" Text="提交"> <DirectEvents > <Click OnEvent="btnSubClick" /> </DirectEvents> </ext:Button> </Items> </ext:Panel> <ext:TaskManager ID="TaskManager1" runat="server"> <Tasks> <ext:Task TaskID="Task1" Interval="5000" AutoRun="false" > <DirectEvents> <Update OnEvent="RefreshProgress" /> </DirectEvents> </ext:Task> </Tasks> </ext:TaskManager> </form>
protected void btnSubClick(object sender, DirectEventArgs e) { X.Msg.Show(new MessageBoxConfig { Title = "请稍候", Message = "正在上传", ProgressText = ".", Width = 300, Progress = true, Closable = false, AnimEl = this.btnSub.ClientID, }); this.StartLongAction(); } private void StartLongAction() { this.Session["complete"] = 0; ThreadPool.QueueUserWorkItem(LongAction); this.TaskManager1.StartTask("Task1"); } private void LongAction(object state) { //Common.CommonHelper.UpLoadFile(FileUploadField3,"/UpLoadImages",Convert.ToInt32(this.Session["complete"]),Convert.ToInt32(this.Session["allByte"])); if (fl1.HasFile) { string openPath = ""; if (fl1.PostedFile != null && fl1.PostedFile.ContentLength > 0) { openPath = fl1.PostedFile.FileName; string strPictureName = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复 if (!string.IsNullOrEmpty(openPath)) { try { string strFileName = System.IO.Path.GetFileName(openPath); strPictureName = DateTime.Now.Ticks.ToString() + strFileName; string strMapPath = Server.MapPath("~/media/"); string strPath = strMapPath + strPictureName; //文件总大小this.Session["allByte"]= fl1.PostedFile.ContentLength; FileStream fsreader = new FileStream(openPath, FileMode.Open); FileStream fswriter = new FileStream(strPath, FileMode.Create); int readbyte = Convert.ToInt32(this.Session["complete"]); using (fsreader) { using (fswriter) { byte[] byts = new byte[1024]; while (true) { int b = fsreader.Read(byts, 0, byts.Length); readbyte += b; this.Session["complete"] = readbyte; if (b <= 0) { break; } fswriter.Write(byts, 0, b);
Thread.Sleep(1000); } } } } catch (Exception ex) { ExtNet.Msg.Alert("提示", ex.Message).Show(); } } } } this.Session.Remove("complete"); this.Session.Remove("allBtte"); } protected void RefreshProgress(object sender, DirectEventArgs e) { if (this.Session["complete"] != null && this.Session["allByte"] != null) { float progress = float.Parse(this.Session["complete"].ToString()); float all = float.Parse(this.Session["allByte"].ToString()); X.Msg.UpdateProgress(progress / all, string.Format("已上传{0}%", Convert.ToInt32((progress / all) * 100))); } else { this.TaskManager1.StopTask("Task1"); X.MessageBox.Hide(); this.run1.AddScript("Ext.Msg.notify('恭喜你', '已上传成功!');"); } }
上传大文件建议使用 分块上传,flash,silverlight 都有这样的插件。
上传完成后,task还在不停地刷新RefreshProgress()。 这个问题,调试下是 服务器端的问题还是客户端的问题。也有可能是连接超时了。
我调试了很多次都是在RefreshProgress()里,不清楚已经STOP掉了 还在刷新,不知道怎么看是那里的问题,本人新手能否教教我