2012-04-04 56 views
1

如何顯示(使用C#)存儲在asp.net網絡上的圖像。我想IMG SRC要在一個如下:如何顯示在asp.net(C#)存儲在網絡上的圖像

\\pgapp\folder\image.jpg,其中pgapp是網絡上的共享驅動器。

我使用這些代碼,但它不顯示網頁上的圖像。

[email protected]"\\pgapp\folder\image.jpg"; 

originalImage2.Attributes["src"]=ResolveUrl(@"\\pgapp\folder\image.jpg");

<div ID="imgContainer" runat="server" style="width: 400px; height: 400px; overflow:auto; border: solid 1px black; 
        padding: 10px; margin-bottom: 5px;" > 

        &nbsp;<asp:Image ID="originalImage2" runat="server" /> 
       </div> 
+0

哪裏圖片src指向當你做調試? – 2012-04-04 02:00:25

+0

Webforms或MVC? – om471987 2012-04-04 02:36:44

回答

2

你會打2個問題:

  • 瀏覽器通常無法訪問服務器共享(除非它是在同一網絡上)。您需要通過您的網站暴露圖像莫名其妙所以可見像http://myserver/image/image1.jpg
  • 「NTLM一跳」 - 你的服務器將不能夠如果你開始通過您的服務器渲染圖像讀取來自遠程服務器的文件。你只需要驗證你是否有問題並閱讀它。
1

顯示存儲在用戶的網絡瀏覽器上的圖像做類似如下:

轉換爲它的base64對口和顯示圖像:

// PhotoId is the network file path and name. 
string photoId = "\\Photos\2015-May\390828d1-8f20-4fe9-9287-13d03894e9c0.jpg" 

// Call to display the networked images. 
lbl_images.Text += "<img src='" + this.PhotoBase64ImgSrc(photoId) + "' height='60px' width='60px' alt='photo' />"; 

// Supporting function that converts an image to base64. 
protected string PhotoBase64ImgSrc(string fileNameandPath) 
{ 
    byte[] byteArray = File.ReadAllBytes(fileNameandPath); 
    string base64 = Convert.ToBase64String(byteArray); 

    return string.Format("data:image/gif;base64,{0}", base64); 
} 
+0

如何在WebForms中顯示圖像? – sojim2 2016-04-23 00:52:24

+0

這不起作用,因爲現代瀏覽器出於安全原因不允許訪問網站緩存之外的文件。因此,如果它是'C:\ outsidecache \ file.jpg',它將無法工作。我嘗試了'\\ sharedfolder \ file.jpg'和'file:// sharedfolder/file.jpg',但它不起作用。 – sojim2 2016-04-25 16:48:22

+0

@ sojim2我刪除了我的意見,因爲這是錯誤的...我說的是不是windowsforms ... web表單我在一個web表單應用程序中使用這個......你必須擁有訪問的帳戶下運行在IIS應用程序池網絡位置......然後應用程序池可以讀取圖像文件,將其轉換爲Base64並將其發送到瀏覽器作爲一個字符串....然後使用它像 2016-04-26 07:59:47

相關問題