2013-02-27 64 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