ASP.NET - How to avoid file upload value clear

Asked By msakt on 28-May-12 06:25 AM
How to avoid file upload value clear

file upload value is clear when click upload button..i want value in filupload control after click upload button

f
Somesh Yadav replied to msakt on 28-May-12 06:26 AM

The ASP.NET FileUpload control maps to the HTML input element with type="file". This element is considered Read-only and you cannot change it directly.

However, there seem to be atleast three workarounds to accomplish the goal of "clearing the field" :

a. Reset the form, either using script or by providing a input type="reset" button.

b. Re-establish the input field in the DOM by setting its attributes again:

var fu = document.getElementById("fileUpload");
if (fu != null)
{
  fu
.setAttribute("type", "input");
  fu
.setAttribute("type", "file");
}

c. Recreate the innerHTML of the field from the existing innerHTML as http://gusiev.com/2009/04/clear-upload-file-input-field/:

var fu = document.getElementById("fileUpload");
if (fu != null)
{
 
// You may be able to use the cached object in `fu`, but I'm not sure.
  document
.getElementById("fileUpload").innerHTML = fu.innerHTML;
}
Jitendra Faye replied to msakt on 28-May-12 06:28 AM
This is default behaviour of FileUpload control, after postback it lost the file name. but if you want to maintain file name then you can maintain in code behind like this-


ViewState["FileName"] = FileUpload1.PostedFile.FileName;

But you can not assign filename to FileUpload control.
TSN ... replied to msakt on 28-May-12 07:20 AM

Hi
    In order to maintain the state of the file upload control just you the session variables

    //If first time page is submitted and we have file in FileUpload control but not in session
      // Store the values to SEssion Object
      if (Session["FileUpload1"] == null && FileUpload1.HasFile) 
      {
        Session["FileUpload1"] = FileUpload1;
      
      }
      // Next time submit and Session has values but FileUpload is Blank
      // Return the values from session to FileUpload
      else if (Session["FileUpload1"] != null && (! FileUpload1.HasFile))
      {
        FileUpload1 = (FileUpload) Session["FileUpload1"];
        Label1.Text = FileUpload1.FileName;
      }
      // Now there could be another sictution when Session has File but user want to change the file
      // In this case we have to change the file in session object
      else if (FileUpload1.HasFile)
      {
        Session["FileUpload1"] = FileUpload1;
        Label1.Text = FileUpload1.FileName;
      }

Goniey N (Mr. G) replied to msakt on 28-May-12 07:44 AM
Default behavior of the FileUpload control is clear the value.

so you can displays that fileupload value into the lable.

lblFileUpload.Text = FileUpload1.PostedFile.FileName;

This will display the value of posted file in to the label.


There is the only way that you have to store that value into particular variable or control or other as per your need...



Hope that this will clear to you....
dipa ahuja replied to msakt on 28-May-12 08:48 AM
Put the fileupload and button inside the update panel