2

I run a local version of my Web Form in visual studio 2010 with the Ajax File Upload control.

I test by uploading a file and writing the e.fileName, e.fileSize, etc. to a database, and then using a GridView to download the file later.

My issue is this: when I write the e.fileName to the database, it gives me the entire path of the file (i.e. "C:\Folder\filename.xls") as the files name.

Advice on correcting this would be greatly appreciated.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
central
  • 25
  • 1
  • 2
  • 7

1 Answers1

3

You could use Path.GetFileName in System.IO:

string justTheName = Path.GetFileName(e.fileName);

And then pass "justTheName" to the database.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
  • 1
    I had made this change, and it works great! This seems to apply just for writing on the database from a local build/debug of my code. The problem persists when I put this code on the IIS in its virtual folder (i.e. the file name is still 'C:\folder\filename.xls') Thanks for your response but the issue is no longer on my local build but is still being applied when it runs from the server build. – central Jul 30 '12 at 20:18
  • @central Ah, good catch. According to [this answer](http://stackoverflow.com/questions/1130560/get-full-path-of-a-file-with-fileupload-control/3077008#3077008), it might be more beneficial (in your development environment) to use `Server.MapPath` to get the real path, then use `Path.GetFileName`. Something like `string justTheName = Path.GetFileName(Server.MapPath(e.fileName));` – Josh Darnell Jul 30 '12 at 20:38