How to clear file upload text in server side (c#)
Asked Answered
D

4

12

I want to clear the file path from the file upload. The file upload is inside the update panel and I am using a AsyncFileUpload. How can I clear the file and change the background color of the fileupload

btnAudUpload_Click Method

string filename =FileUpload.FileName;
string Fullpath = Path.Combine(@"D:\Media", filename);
if (FileUpload.HasFile)
  { 
 if (filename.ToLower().EndsWith("mp4"))
     {  
      //Saving the file
     }
  else
     {
          //I want to clear the  FileUpload content here
     }    
  }
Drubbing answered 21/12, 2011 at 17:7 Comment(0)
M
13

Clear the Attributes worked for me... but that will remove styles and other stuff

string filename =FileUpload.FileName;
string Fullpath = Path.Combine(@"D:\Media", filename);
if (FileUpload.HasFile)
{ 
  if (filename.ToLower().EndsWith("mp4"))
  {  
     //Saving the file
  }
  else
  {
     //I want to clear the  FileUpload content here
     FileUpload.Attributes.Clear();
  }    
}
Moo answered 16/1, 2013 at 18:22 Comment(1)
I think this is a much more cleaner way of doing it :)!Pelagi
C
7

I know this thread is almost a year old, but this still seems to be a prevalent issue. The easiest fix I've found is to set the file upload control to a new instance of it.

FileUpload1 = new FileUpload();
Copenhagen answered 20/9, 2012 at 3:6 Comment(2)
This definitely gets the job done, although I have to wonder... Do you need to run a Dispose() on the previous control in order to free up its resources?Critchfield
Just a guess, but I would say no. Garbage collection should take care of the cleanup.Copenhagen
T
3

If you want to have interactivity without relouding the page you'll have to use JavaScript. That's why I would check the file extension on the client side instead of the server side. Example:

function checkFile() {
    var input = document.getElementById('fileUpload').value.toLowerCase();
    var extension = '.mp4';

    if (!input.indexOf(extension, input.length - extension.length) != -1) {
        alert('Invalid file extension. Only .mp4 is allowed.');
        document.getElementById('fileUpload').value = '';
    }
}

The only thing you'll have to add is changing the fileUpload background color which is very easy to do.

Good luck!

Translatable answered 22/12, 2011 at 0:4 Comment(1)
question: how to change the background color, where is the solution, you posted code which not required, stop placing use less answersLycian
S
1

I think when you do postback the file contnet property will removed by default, because a security reasons !

Sheers answered 27/2, 2013 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.