2013-03-19 99 views
1

我在C#中创建了一个MVC3 Web应用程序。 我必须实现一个搜索屏幕来显示来自SQL数据库的数据和与这些数据相对应的图片。 在我的个人资料页我创建了一个链接到该文档:创建包含百分比符号的文件的链接

 @{ 
     string fullDocumentPath = "~/History/" + Model.PICTURE_PATH + "/" + Model.PICTURE_NAME.Replace("001", "TIF"); 
    } 
    @if (File.Exists(Server.MapPath(fullDocumentPath))) 
    { 
     <a href="@Url.Content(fullDocumentPath)" >Click me for the invoice picture.</a> 
    } 

的问题是,谁创建的文档(并增加了一个参考的数据库路径)的系统选择在许多使用%的文件名称。 当我有这个链接:http://localhost:49823/History/044/00/aaau2vab.TIF这没关系。在创建此链接:http://localhost:49823/History/132/18/aagn%8ab.TIF它失败:

The resource cannot be found. 
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. 
Requested URL: /History/132/18/aagn�b.TIF 

我该如何解决这个问题?

回答

0

使用Url.Encode()方法逃避scpecial字符:

@{ 
    string documentDirectoryPath = "~/History/" + Model.PICTURE_PATH + "/"; 
    string documentName = Model.PICTURE_NAME.Replace("001", "TIF"); 
} 
@if (File.Exists(Server.MapPath(documentDirectoryPath + documentName))) 
{ 
    <a href="@Url.Content(documentDirectoryPath + Url.Encode(documentName))" >Click me for the invoice picture.</a> 
} 
+0

它会创建类似于“http:// localhost:49823/BusinessCaseHistory/Details /〜%2fHistory%2f132%2f18%2faagn%258ab.TIF”的内容,导致HTTP Error 400 - Bad Request。 – user2185692 2013-03-19 09:31:16

+0

对,那么你将不得不编码字符串的名称部分 – Dima 2013-03-19 09:36:47

+0

这次它给了我'http:// localhost:49823/History/132/18/aagn%258ab.TIF',然后HTTP Error 400 - 错误的请求。如果文件名被修改,那么它就不能被找到。我担心我必须重新命名数据库中的所有文件和记录(其中约有40万)。 – user2185692 2013-03-19 09:48:07

0

您试图访问的URL不是URL编码。你只能使用ASCII字符,所以你需要UrlEncode你的路径。你可以看到这些字符在此列表中的列表,以及对应的ASCII字符:

http://www.w3schools.com/tags/ref_urlencode.asp

你可以把你的路径字符串中使用的以UrlEncode方法进行URL编码:

http://msdn.microsoft.com/en-us/library/zttxte6w.aspx

如果你想要再次进行解码,可以使用UrlDecode方法:

http://msdn.microsoft.com/en-us/library/6196h3wt.aspx

+0

据我了解,不仅在数据库中的名称,而且文件系统中的文件都带有%的名称,所以如果您尝试解码名称,最终会输入错误的名称。 – Dima 2013-03-19 09:32:05

+0

在数据库中,我有一列(如'132/21')中的文件路径和另一列(如'aagn%8ab.001')中的文件名。所以,没有网址首先解码。 – user2185692 2013-03-19 09:34:45

+0

@Dima是正确的。 – user2185692 2013-03-19 09:35:31

相关问题