0

I'm using Request.Files to obtain a file that the user is uploading on my web page.

I noticed that if I use the filename property in IIS it gives me a path + filename, however if I run in cassini it only gives me the filename no matter what directory I use.

Why is this? And, is there a way to just use the filename when in IIS?

Thanks, rod.

Rod
  • 14,529
  • 31
  • 118
  • 230
  • Duplicate question: http://stackoverflow.com/questions/382464/httppostedfile-filename-different-from-ie – Chris Jun 25 '10 at 16:49
  • Ok, the question is slightly different, but the issue is the same. Making the full client path available to the server script is a potential security risk. Newer browsers only send the filename, not the path, when uploading a file. – Chris Jun 25 '10 at 16:50

1 Answers1

1

To get the file name only use:

System.IO.Path.GetFileName(userPostedFile.FileName));

like:

HttpFileCollection uploadedFiles = Request.Files;

   for (int i = 0; i < uploadedFiles.Count; i++)
   {    
      HttpPostedFile userPostedFile = uploadedFiles[i];

      if (userPostedFile.ContentLength > 0 )
      {
        string fileName = System.IO.Path.GetFileName(userPostedFile.FileName));
      }


   }
VoodooChild
  • 9,776
  • 8
  • 66
  • 99