Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IO.File.Exists() always returns false

Tags:

asp.net

vb.net

Consider the following code in Button1_Click

Dim stFile as String = IO.Path.Combine(Server.MapPath("~/"), "uploads/text/file1.txt")
If IO.File.Exists(stFile) Then
    ' Do some processing
End If

Exists always returns false in the above code block

And here is Button2_Click code block

Dim stFile as String = IO.Path.Combine(Server.MapPath("~/"), "uploads/text/file1.txt")
Response.Clear()
Response.ContentType = "text/plain"
Response.AppendHeader("content-disposition", "attachment;filename=abc.txt")
Response.TransmitFile(stFile)
Response.Flush()
End If

This always downloads the same file. What could be the problem?

like image 807
Johny Avatar asked Mar 21 '26 08:03

Johny


1 Answers

I also crumbled with this issue a while ago and found that the use of "/" and special chars may produce this scenario.

Path.Combine always returns paths with "\".

Try changing uploads/text/file1.txt to uploads\text\file1.txt

If you are generating dynamic file names then try to avoid including any special characters which may require url encoding such as %, (, [space] etc.

(Some concepts may seem illogical in this post but using the combination of \, / and special chars wasted almost 8-10 hours of mine)

like image 125
haraman Avatar answered Mar 23 '26 23:03

haraman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!