从 Web 目录中显示图像



使用 VS 2015 和 ASP.net/VB。

在我的桌面上,我正在构建一个保存在远程服务器(开发服务器)上的 Web 应用程序。 如果我使用 VS 运行网站,那么一切正常。

我想向用户演示 Web 应用程序,但页面上的图像出现问题。 我确信我错过了一些简单的东西,但我无法在网上找到解决方案。 当我演示它时,我使用服务器 (http://DevelopmentServer/myName/website1/default.aspx) 的 url,当我在我的 PC 上运行它时,它的本地主机等。

在页面中,我有一个上传文件控件,用户可以使用它来上传图像。 完成后,图像将显示在屏幕上。 我遇到的问题是文件路径。 要上传图像,我使用以下代码。

Protected Sub upLoadFile(sender As Object, e As EventArgs)
Dim fileExt As String = Path.GetExtension(chartUpload.PostedFile.FileName)
Dim folderPath As String = "\developmentServerwebsite1charts"
Dim fileName As String = "Chart_" & Date.Now.ToString("ddMMyyyy")
Dim updatedFilename As String = fileName & fileExt
chartUpload.SaveAs(Path.Combine(folderPath & updatedFilename))
displayCurrentChart()
End Sub

使用 url 时,上述工作正常 - http://DevelopmentServer/myName/website1/default.aspx

当我想使用以下代码显示图像时,会出现此问题:

Private Sub displayCurrentChart()
Dim chartName As String = "Chart_" & Date.Now.ToString("ddMMyyyy") & ".jpg"
Dim folderPath As String = "D:DevelopementWebsite1charts"
Dim fullFilePath As String = folderPath & chartName
chartImage.Visible = False
If My.Computer.FileSystem.FileExists(fullFilePath) Then
chartImage.ImageUrl = fullFilePath
chartImage.Visible = True
Else
'do nothing
End If
End Sub

我只是得到一个黑色的十字架。 我确信这与我正在使用的路径有关,因为显然用户没有 D 驱动器,但如果我使用 UNC 路径,它也不起作用

\developmentServerwebsite1charts.

有什么建议吗?

代码中的一些问题/建议可能会导致问题:

保存时,
  • 使用用户提供的扩展名保存文件,但在显示时,您假设它是".jpg">
  • Path.Combine(folderPath & updatedFilename)应该Path.Combine(folderPath, updatedFilename)
  • 使用Server.MapPath获取正确的路径,例如Server.MapPath("/charts")/charts是否位于 Web 应用程序的根目录中

我建议您在这些方法中设置断点,以查看调试应用程序时的实际路径是什么。然后检查该路径中是否存在图像。

最新更新