IO.File.Exists()始终返回false

时间:2015-10-14 09:27:17

标签: asp.net vb.net

在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总是在上面的代码块中返回false

这里是Button2_Click代码块

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

这始终会下载相同的文件。可能是什么问题?

1 个答案:

答案 0 :(得分:2)

我刚才也解决了这个问题,并发现使用“/”和特殊字符可能会产生这种情况。

Path.Combine始终返回带有“\”的路径。

尝试将uploads\text\file1.txt更改为%

如果您要生成动态文件名,请尽量避免包含可能需要网址编码的任何特殊字符,例如([space] NSLog(@"selected segment: %li", _segmentControl.selectedSegmentIndex); 等。

在这篇文章中,有些概念可能看起来不合逻辑,但使用\,/和特殊字符的组合浪费了近8​​到10个小时

相关问题