2014-12-22 23 views
0

當文件名中有空格時,以下代碼無法按預期工作。 Uri.AbsolutePath逃脫字符串,從而映射路徑上盤還包括不必要的轉義字符:在URI上使用Server.MapPath時處理轉義空格

var uri = new Uri("http://localhost/my file.txt"); 
var pathOnDisk = HttpContext.Current.Server.MapPath(uri.AbsolutePath); 
// Resulting incorrectly in "C:\MyWebsite\my%20file.txt" 

無碴左右,而使用官方的解決方案不與string.replace,一個人如何可以從轉義路徑uri,或者將路徑映射到磁盤而不使用轉義字符?

+0

你會找到這裏的空間是子串並用的String.Empty替換 – SuncoastOwner

回答

0

只是對其進行解碼:

var uri = new Uri("http://localhost/my file.txt"); 
var pathOnDisk = HttpContext.Current.Server.MapPath(uri.AbsolutePath); 
var decoded = HttpContext.Current.Server.UrlDecode(pathOnDisk);