简体   繁体   中英

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

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
     }    
  }

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();
  }    
}

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();

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!

我认为当你做回发文件时,默认情况下会删除contnet属性,因为出于安全原因!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM