|
Hello All,
I'm try to show images from database in a client side, but, I have been using many kinds of examples that I found and none work, none of the images shown. The last example I saw it's this:
Auto-bind byte[] to asp:Image[^]
I understand perfectly the example, but the images are still not displayed.
Someone help me a bit with this issue?
In summary, I'm use a HTML5 drag & drop files. Send the files via XMLHttpRequest in a FormData object. One handler take this files in byte() and store in SQL DataBase.
Client code:
$("#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);
};
Handle code:
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
More late, I try to put this byte() in a image tag on grid.
aspx code:
<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>
</asp:GridView>
And the function GetImage in codebehind:
Public Function GetImage(image As Object) As String
Return "data:image/gif;base64," & Convert.ToBase64String(DirectCast(image, [Byte]()))
End Function
In all steps, there is no error, but I think that the error is posible located in the byte() file format..but i'm not sure.
Someone help me?
Sorry for my inglish and Thanks.
|
|
|
|
|
does your image really have the content type of 'image/gif'? (in other words does it have the extension .gif? )
Help people,so poeple can help you.
|
|
|
|