2013-02-27 63 views
0

我試圖從客戶端的數據庫中顯示圖像,但是,我一直在使用多種示例,我發現並且沒有任何工作,沒有顯示任何圖像。我看到的最後一個例子是這樣的: http://www.codeproject.com/Tips/445876/Auto-bind-byte-to-asp-Image圖像標記上顯示圖像字節()的問題

我完全理解這個例子,但圖片仍然沒有顯示。

有人幫我解決了這個問題嗎?

總之,我使用HTML5拖動&拖放文件。通過FormData對象中的XMLHttpRequest發送文件。一個處理程序在byte()中獲取此文件並存儲在SQL DataBase中。

客戶端代碼:

$("#btnUploadFile").click(function() { 
    if (files.length <= 0) { 
     alert("Debe seleccionar algún fichero para subirlo."); 
    } else { 
     var expID = $("#ContentPlaceHolder1_hfExpId").val(); 
     var formData = new FormData(); 
     for (var i = 0; i < files.length; i++) { 
      alert(files[i].name); 
      formData.append('file', files[i]); 
     } 
     var xhr = new XMLHttpRequest(); 
     xhr.open('POST', "FileHandler.ashx", true); 
     xhr.overrideMimeType('text/plain; charset=x-user-defined-binary'); 
     xhr.setRequestHeader("ExpedienteID", expID); 
     xhr.onload = function() { 
      if (xhr.status === 200) { 
       RefreshFilePanel(); 
      } else { 
       console.log('Something went terribly wrong...'); 
      } 
     }; 
     xhr.send(formData); 
    }; 

處理代碼:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 
    Dim documentBytes As Byte() 
    Dim lExpId As String = context.Request.Headers("ExpedienteID") 
    Dim fLenght As Integer 

    If (context.Request.Files.Count > 0) Then 
     Dim files As HttpFileCollection = context.Request.Files 

     For i = 0 To files.Count - 1 
      fLenght = files(i).ContentLength 
      documentBytes = New Byte(fLenght - 1) {} 
      context.Request.InputStream.Read(documentBytes, 0, fLenght) 
      UploadDocumentBy_ExpID(documentBytes, files(i).FileName, "Description" & lExpId.ToString, lExpId) 
     Next 
    End If 
End Sub 

更晚,我儘量把這個字節()的圖像標籤上的網格。

ASPX代碼:

<asp:GridView ID="grdDocumentoByExp" runat="server" AutoGenerateColumns="false" Width="250px" DataSourceID="dtsDocumentByExpId"> 
<Columns> 
    <asp:BoundField DataField="Archivo" HeaderText="Archivo" /> 
    <asp:BoundField DataField="docId" HeaderText="docId" /> 
    <asp:BoundField DataField="Documento" HeaderText="Documento" Visible="false" /> 
    <asp:TemplateField HeaderText="Preview"> 
     <ItemTemplate> 
      <asp:Image 
       ID="imgThumb" 
       runat="server" 
       ImageUrl='<%# GetImage(Eval("Documento")) %>' 
       /> 
     </ItemTemplate> 
    </asp:TemplateField> 

</Columns> 

而在代碼隱藏功能的getImage:

Public Function GetImage(image As Object) As String 
    Return "data:image/gif;base64," & Convert.ToBase64String(DirectCast(image, [Byte]())) 
End Function 

在所有的步驟,沒有錯誤,但我認爲,錯誤是posible位於字節()文件格式..但我不知道。

有人幫我嗎?

對不起,我的英文和謝謝。

回答

0

的問題是你如何讀文件:

context.Request.InputStream.Read(documentBytes, 0, fLenght) 

Read方法返回讀取的字節,它可以是小於您請求的字節數的數量。你必須循環,直到你得到所有字節:

Dim offset as Integer = 0 
Dim len as Integer 
Do While offset < fLength 
    len = context.Request.InputStream.Read(documentBytes, offset, fLenght - offset) 
    offset += len 
Loop 
+0

不要工作太多。 – amelian 2013-02-27 15:10:47

+0

@AMGHoshi:這是一個GIF圖像嗎?最終的src是什麼樣的? – Guffa 2013-02-27 15:18:28