2010-11-02 186 views
1

我有一个页面,允许用户上传照片,照片的路径将保存在分贝,这是这样的'〜/ images/1288598614_house - Copy_000002.png'检查文件是否存在

所以,我想检查是用户检索照片时存在的文件。

我曾尝试下面的代码:

Dim myPhoto As String = ~/images/1288598614_house - Copy_000002.png 

If File.Exists(myPhoto) Then 
    hfPhotoUploadPath.Value = myPhoto 
    imgPhoto.ImageUrl = hfPhotoUploadPath.Value 
Else 
    imgPhoto.ImageUrl = "~/images/default.jpg" 
End If 

,但它不工作.....

+0

不应该被替换为根路径? – Doggett 2010-11-02 08:02:52

回答

1

你可能想将其映射到文件路径(使用C#语法的示例):

string localPath = Server.MapPath(myPhoto); 
if(File.Exists(localPath)) {...} 

然而 - 赤裸裸的文件系统不是一定此数据是最好的选择 - 或者至少,你需要允许某些文件类型的上传前消毒它。此外,你可能(取决于规模)需要考虑多台服务器等。

2

您需要更换~Server.MapPath("~")

Dim rootPath As String = Server.MapPath("~") 
+0

正确,〜仅适用于显示给浏览器的路径,不适用于本地文件路径。 – 2010-11-02 08:03:52