0

I am uploading a file with

var filename = Server.MapPath(Path.Combine("~/Content/UserContent", Path.ChangeExtension(newName,Path.GetExtension(attachments.FileName))));
attachments.SaveAs(filename);

it works great except that in Internet Explorer it gives the full path "C:\Users\okke\Desktop\GEWOONEENMAP OK\etags.txt" instead of just saying "etags.txt", how can I fix this?

tereško
  • 58,060
  • 25
  • 98
  • 150
Conceptual
  • 57
  • 2
  • 12

1 Answers1

3

Call Path.GetFileName on the result to get only the file name e.g.

attachments.SaveAs(Path.GetFileName(fileName));

If the value of fileName is a file path it will return the file name (with ext), if it is already a valid file name it will just return the same value.

James
  • 80,725
  • 18
  • 167
  • 237
  • Yes but now it says SaveAs requires a rooted path, when I am saving the file to the database (just the name), it saves the whole path name except just "etags.txt" – Conceptual Dec 19 '12 at 13:22
  • Well that wasn't your original question, however, to fix it all you need to do is wrap your `Path.ChangeExtension` call with `Path.GetFileName` instead of in the `SaveAs` call. – James Dec 19 '12 at 13:40