开发者

Check if file exists

开发者 https://www.devze.com 2023-01-22 20:27 出处:网络
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\'.

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

精彩评论

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

关注公众号