Question

I am using a asyncfileupload control to upload a file there i am taking the path in a view state like this:

protected void ProcessUpload(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    string name = System.IO.Path.GetFileName(e.FileName); 
    string dir = Server.MapPath("upload_eng/");
    string path = Path.Combine(dir, name);
    ViewState["path"] = path;
    engcertfupld.SaveAs(path);
}

Now when i am trying to save that path in a buttonclick event i am not getting the value of viewstate:

protected void btnUpdate_Click(object sender, EventArgs e)
{
   string filepath = ViewState["path"].ToString(); // GETTING NULL in filepath
}

In this filepath i am getting null actually i am getting error NULL REFERENCE EXCEPTION

What can I do now?

Était-ce utile?

La solution

Put the Path value in the Session object instead of the ViewState, like this:

protected void ProcessUpload(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{

    ....
    string path = Path.Combine(dir, name);
    Session["path"] = path;
}

Then in the Button Click:

protected void btnUpdate_Click(object sender, EventArgs e)
{
  if (Session["path"] != null)
  {
     string filepath = (string) Session["path"];
  }
}

Autres conseils

I guess the upload process is not a "real" postback, so the ViewState will not be refreshed client side and won't contain the path upon click on btnUpdate_Click

What you should do is use the OnClientUploadComplete client-side event to retrieve the uploaded file name, and store it in a HiddenField that will be posted on the server on btnUpdate_Click.

Here is a complete example where the uploaded file name is used to display an uploaded image without post-back :

http://www.aspsnippets.com/Articles/Display-image-after-upload-without-page-refresh-or-postback-using-ASP.Net-AsyncFileUpload-Control.aspx

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top