How to give a POST on the page with the UPLOAD property without going into looping?

Asked

Viewed 48 times

0

Looking on the internet I saw some websites saying that when you own a property Upload on the page can not give POST the same because it goes into looping.

I have an application that creates/deletes folders and displays an alert message (server) where a POST on the page thus making enter the looping, and thus returning error.

inserir a descrição da imagem aqui

My question would be, how to give a POST on the page in order to update the form information without entering it looping?

Example of the Load Fileupload File event:

protected void BtnCarregar_Click(object sender, EventArgs e)
    {
        try
        {
            string caminho = ConfigurationManager.AppSettings["PastaRaiz"];
            var source = Directory.GetDirectories(caminho).Select(c => new DirectoryInfo(c).Name).ToList();
            if (ddlFolders.SelectedValue != null)
            {
                if (FileUpload1.HasFile)
                {
                    string fileName = FileUpload1.FileName;
                    if (!Directory.Exists(caminho + ddlFolders.SelectedValue + "/" + fileName))
                    {
                        FileUpload1.PostedFile.SaveAs(caminho + ddlFolders.SelectedValue + "/" + fileName);
                    }
                    else
                    {
                        mensagens.ExibirMensagem("Mensagem", "Arquivo já existente no diretório.", false, this.Page, this.GetType());
                    }
                }
            }
            mensagens.ExibirMensagem("Mensagem", "Arquivo  carregado com sucesso.", false, this.Page, this.GetType());

        }
        catch (Exception ex)
        {

            throw ex;
        }

    }
  • Have some example code to put in your question?

  • @Don'tPanic added

  • What is the error shown?

  • @Leandroangelo It’s not exactly a mistake, but I give POST on this page she gets in a looping at this event. But if I do not give the POST I can not update the form, pq everything is done via Server side

  • It seems to me you’re confusing loot with delay in response. If the user is uploading a file, the request will be "suspended" until the end of this load

  • @Leandroangelo is not that. I do not save in the database, so do not need to do data conversion. It is direct to a physical directory. I added the question to the image that returns to me when I try to refresh the page.

  • This image is from when you try to refresh the page of a form that has already been posted and there was no redirect to a different destination. In your case, by the click event, I suppose it comes to webform and a postback.

Show 2 more comments

1 answer

1


As stated in the comments, this happens because by clicking the button bntCarregar you are invoking an event in the backend, which in turn is triggered by a post on the client side, the famous Webforms Postback. In the browser it is a common Submit, but in Asp.net this can trigger other events that change components and which in turn may be configured to perform a new postback when they have their status changed.

You should add the appropriate treatment in the method Page_Load() of your page. And or map what is chaining this loop.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //... Tratamentos necessários para o carregamento correto da sua página
    }
}

Note: My suspicion is that the "loop" is related to the method ExibirMensagem(), once he gets the this.Page as a parameter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.