Link to home
Start Free TrialLog in
Avatar of RichardRiga
RichardRiga

asked on

Using AJAX.NET how do you clear an uploadfile control?

I have a regular upload file control inside an AJAX.NET updatepanel.  If I use it to upload a file once, its no problem; however, if i hit the submit button again, even though the text box is clear, it is still attached to the previous file uploaded.

How do I clear that so that I may use the uploadfile control freshly?
ASKER CERTIFIED SOLUTION
Avatar of Kamal Khaleefa
Kamal Khaleefa
Flag of Kuwait image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RichardRiga
RichardRiga

ASKER

That's a neat article but how do I call a javascript function after the .NET webservice code has completed its work?
regarding to Ur  question
but a JavaScript function to see if the file upload is empty

 var FileUpload1="<%=FileUpload1.ClientID %>"
function CheckForTestFile()
   {
        var file = document.getElementById(FileUpload1);
        var fileName=file.value;        
        //Checking for file browsed or not
        if (Trim(fileName) =='' )
        {
            alert("Please select a file to upload!!!");
            file.focus();
            return false;
        }
 }
and on ur button
add this

 <asp:Button ID="btnUpload" runat="server"  OnClientClick="if(CheckForTestFile()){return true}else{return false}"
                                                  text="Upload File" TabIndex="50" EnableViewState="False" />
                                           
These methods don't seem to work still.  I have inserted them into the proper places and they are being executed; however, the fileupload control still reports that it has contents when trying to upload a blank file'
My apologies!!  I completely forgot to mention that I wasn't using an ASP:FileUpload control.  I'm using the AsyncFileUpload control from the AjaxToolkit.  I'm not sure it makes a difference in your answers;however, I should have added that.
SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is what happens:

1.  I use the control to upload a file.  That works, no problem.  
     At this point the control's field resets itself to appear empty.
     Since the field is empty, at this point, if I hit the submit button again, the property fuFileUpload.HasFile should be False.  But its not.  It returns true.

2.  So i use a script:
            function clearContents() {
                 var AsyncFileUpload = $get("<%=fuFileUpload.ClientID%>");
                 var txts = AsyncFileUpload.getElementsByTagName("input");
                 for (var i = 0; i < txts.length; i++) {
                     if (txts[i].type == "text") {
                         txts[i].value = "";
                         txts[i].style.backgroundColor = "white";
                     }
                 }
             }

and call on that script when the file is uploaded with:
<cc1:AsyncFileUpload ID="fuFileUpload" runat="server" OnClientUploadComplete="<%UploadComplete() %>" />

Codebehind has:
    Public Sub CallUploadComplete()
        ClientScript.RegisterStartupScript(Me.GetType(), "removeDoc", "ClearContents()", True)
    End Sub

Something isn't right.  Can anyone fix those three snippets of code to make it work?  I beg!  
And thanks.

Open in new window

What does the

IsNothing(fuFileUpload.PostedFile)

return?
Project was scrapped.  Apologies for the waste of time, but thank you for being respondent