Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Posted on 2011-02-15
Last Modified: 2012-06-27
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?
Question by:RichardRiga
  • 5
  • 2
  • 2
LVL 16

Accepted Solution

Kamal Khaleefa earned 250 total points
ID: 34904109

Author Comment

ID: 34910571
That's a neat article but how do I call a javascript function after the .NET webservice code has completed its work?
LVL 16

Expert Comment

by:Kamal Khaleefa
ID: 34914764
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!!!");
            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" />
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 34916726
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'

Author Comment

ID: 34917036
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.
LVL 83

Assisted Solution

CodeCruiser earned 250 total points
ID: 34917067
>however, the fileupload control still reports that it has contents when trying to upload a blank file'
Do you mean you are uploading a 0 byte file?

Author Comment

ID: 34918554
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

LVL 83

Expert Comment

ID: 34924555
What does the



Author Closing Comment

ID: 35028696
Project was scrapped.  Apologies for the waste of time, but thank you for being respondent

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hbo knew my windows software 4 62
Passing data between Forms 3 20
Ajax php search with multiple text fields generated by php 38 65
Where is this file? 3 25
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question