ASP.NET MVC Server.MapPath giving full path in Int

2019-03-04 03:20发布

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?

1条回答
迷人小祖宗
2楼-- · 2019-03-04 03:36

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.

查看更多
登录 后发表回答