2017-06-20 146 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應用

的根,我建議你設置在這些方法中斷點,看看實際的路徑是什麼樣的調試應用程序時。然後檢查圖像是否存在於該路徑中。