Progressbar in ASP.Net - Help

Mar 1, 2012 at 11:40 AM

Hi all, 

I've been fiddling with DotNetZip and tried to reproduce something like this http://dotnetzip.codeplex.com/discussions/277113#mce_temp_url#

My code is simpler and it works, but not completely: I can see the zip process starts and finishes (so it shows me the start message and end message, based on ZipProgressEventType.Saving_Started and ZipProgressEventType.Saving_Completed). It does not show me any progress, though (I'm basing it on ZipProgressEventType.Saving_AfterWriteEntry)

It seems to me the SaveProgress event is not firing or can't reach the method associated with it. 

There's some code below (yes, I'm desperate enough to base the variables on Session).

the button which triggers the zip process:

    protected void btnSalvar_Click(object sender, EventArgs e)
    {
        using (ZipFile zip = new ZipFile())
        {
            string strDiretorioMapeado = Server.MapPath("~/Arquivos/Exportacoes/");
            zip.AddDirectory(strDiretorioMapeado + this.ddlDiretorios.SelectedValue);
            zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
            zip.SaveProgress += new EventHandler<SaveProgressEventArgs>(zip_SaveProgress);
            zip.Save(strDiretorioMapeado + this.ddlDiretorios.SelectedItem.Text + ".zip");
        }
    }

the zip_SaveProgress method:

    private void zip_SaveProgress(object sender, SaveProgressEventArgs args)
    {
        if (args.EventType == ZipProgressEventType.Saving_Started)
        {
            //this.progressoIniciado = true;
            if (Session["progresso"] != null)
                Session["progresso"] = "Iniciado";
            else
                Session.Add("progresso", "Iniciado");
                     
        }
        else if (args.EventType == ZipProgressEventType.Saving_Completed)
        {
                Session["progresso"] = "Finalizado";
        }
        else if(args.EventType == ZipProgressEventType.Reading_Started)
            this.litMsg.Text = "Lendo diretorio...";
        else if (args.EventType == ZipProgressEventType.Saving_AfterWriteEntry)
        {
            this.progressoArquivoAtual = args.CurrentEntry.FileName;
            this.arquivosTransferidos = args.EntriesSaved;
            this.totalArquivos = args.EntriesTotal;
            this.progressoPorcento = (double)args.EntriesSaved / args.EntriesTotal;
            if (Session["progresso"] != null)
                Session["porcento"] = progressoPorcento.ToString("#0.##%");
            else
                Session.Add("porcento", progressoPorcento.ToString("#0.##%")); 
                       
        }
    }

and the ticker which checks the server every one second:

    protected void tmrIntervalo_Tick(object sender, EventArgs e)
    {
        if (Session["progresso"] != null)
        {
            if (Session["progresso"] == "Iniciado")
            {
                this.litMsg.Text = "Iniciando...";
                this.litMsg.Text = String.Format("{0} transferidos...", Session["porcento"].ToString());
                Session["progresso"] = "Em progresso";
            }
            else if (Session["progresso"] == "Finalizado")
            {
                this.litMsg.Text = "Finalizado";
                Session["progresso"] = null;
                Session["porcento"] = "0%";
                this.lstArquivosZip.DataBind();
            }
            else if (Session["progresso"] == "Em progresso")
            {
                this.litMsg.Text = "Total: " + Session["porcento"].ToString();
            }
        }
        else
        {
            this.litMsg.Text = "Nenhum Zip iniciado ainda.";
            Session["porcento"] = "0%";
        }

if anyone could help indicating which way to go from here, I'd be thankful. 

cheers, 

Mar 1, 2012 at 3:14 PM

I've run a series of simple tests here and apparently, when running locally, I only get the SAVING_COMPLETED event. 

I'm a bit frustrated, but my understanding is that my little knowledge of the web architecture is keeping me from figuring it out. :(

Mar 2, 2012 at 10:33 AM
marcosaccioly wrote:

I've run a series of simple tests here and apparently, when running locally, I only get the SAVING_COMPLETED event. 

I'm a bit frustrated, but my understanding is that my little knowledge of the web architecture is keeping me from figuring it out. :(

I believe this happens because locally things happen too fast for the browser to update via Ajax. Even though I've been trying to capture the events into session variables in order to display them to the browser window I'm not able to see the changes (even though I have used large folders for testing).

Remotely, though, I was able to grab the percentage. However, I believe that due to client/server relationships not all the event types (ZipProgressEventType). As I'm hitting the server every 500ms I believe the event type is a bit random (hundreds of these events can happen in that time). 

So, to sum up: I was able to get the percentage and the progress across the Internet (though I get some NaN and have to deal with them).