views:

416

answers:

1

I'm working with some code that uploads an image from a form and stores it on our server. In Internet Explorer the user can enter a path manually, and I wonder how I can check that the file exists, i.e., that the user entered a valid path.

There's a FileItem object that's being used to check size (e.g., fileItem.getSize() < MAX_SIZE), and I wonder if a good approach would be to use size to check that the file exists. For example:

if (fileItem.getSize() == 0) {
  // Somethings wrong -- invalid path.
} else {
  // File exists -- valid path.
}

Any suggestions are appreciated. Thanks!

+1  A: 

On the client, you cannot reliably read the text of a file upload control with script. IE8 and Opera10, for instance, will lie to you and provide a generic path containing "C:\fakepath\". This is done for privacy reasons.

On the server, you can do exactly as you've done, simply check to see that you actually got a file in the upload, and if so, then you can examine the file, determine if it matches your criteria.

EricLaw -MSFT-