2017-06-20 145 views
0

使用VS 2015和ASP.net/VB。显示网络目录中的图像

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

我想演示Web应用程序给用户,我遇到了页面上的图像问题。我确定有一些东西很容易丢失,但我无法找到解决方案。当我演示它时,我使用一个URL到服务器(http://DevelopmentServer/myName/website1/default.aspx),当我在我的PC上运行它的本地主机等。

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

Protected Sub upLoadFile(sender As Object, e As EventArgs) 

    Dim fileExt As String = Path.GetExtension(chartUpload.PostedFile.FileName) 
    Dim folderPath As String = "\\developmentServer\website1\charts\" 
    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:\Developement\Website1\charts\" 
    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路径

\\developmentServer\website1\charts\也不起作用。

有什么建议吗?

回答

0

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

  • 时为您节省存为扩展用户suplied文件,但会显示您假定它的时候是“.JPG”
  • Path.Combine(folderPath & updatedFilename)应该是Path.Combine(folderPath, updatedFilename)
  • 使用Server.MapPath以获得正确的路径eg Server.MapPath("/charts")如果/charts是在你的web应用

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