开发者

Check if file exists

I have a page that allow user to upload a photo and the path of the photo would be save in db, which is something like this '~/images/1288598614_house - Copy_000002.png'.

So, i would like to check is the file exists when user retrieve the photo.

I have tried the code bel开发者_如何学JAVAow:

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

but it's not working.....


you need to replace the ~ with Server.MapPath("~")

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


You probably want to map that to a file path (example using C# syntax):

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

however - the naked file system isn't necessarily the best option for this data - or at least, you would need to sanitize it before allowing upload of some file types. Also, you might (depending on scale) need to think about multiple servers, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜