Ev Conrad Posted January 28, 2003 Posted January 28, 2003 I have a web application written in ASP.NET that allows users to select records from a database. Some of these records include image data. I would like to deliver the image content to the user without having to write them to the disk. Is there a way to 'stream' the images via http without requiring them to be written to disk? Thanks! Everett Quote
Moderators Robby Posted January 28, 2003 Moderators Posted January 28, 2003 Here's something that I have used in the past, I stripped it down to only what you may need. (I did it for something other than images, but this should work) Private Function GetAttach(ByVal nID As Integer) As Byte() Dim oImage() As Byte 'use nID in the WHERE clause of the SQL statement oImage = CType(yourDataReader.Item("SomeImageField"), Byte()) Return oImage End Function Private Sub GetDownload(ByVal nID As Integer) Try Dim sFile As String = "SomeDefaultName" 'you can use a function to acquire the name Response.Clear() Response.ContentType = "image/JPEG" Response.AppendHeader("Content-Disposition", "filename=" & sFile & ".jpg") Dim oImage() As Byte = GetAttach(nID) Response.BinaryWrite(oImage) Catch Response.Clear() Finally Response.Flush() Response.Close() End Try End Sub Quote Visit...Bassic Software
Ev Conrad Posted January 28, 2003 Author Posted January 28, 2003 Thanks. A couple of questions: Since this will be binary data, can I use any name as a file name? If I want the image to be on the page in addition to normal text data, can I switch the content type back & forth? I couldn't make my first pass work, but I'll try again tonight or tomorrow morning and see how it goes. Thanks again. Everett Quote
Moderators Robby Posted January 28, 2003 Moderators Posted January 28, 2003 I use the above code to allow users to download files from a database, AppendHeader is to give a default name to the file as the user is saving in a SaveAs situation... I don't know how it will act while displaying images as I haven't tried it. (for images) Quote Visit...Bassic Software
Moderators Robby Posted January 28, 2003 Moderators Posted January 28, 2003 I tested it and it works, I used almost the same method, here's the complete code.... 'ASPX code..... <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="TestImageFromDatabaseSQL.WebForm1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>WebForm1</title> <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR"> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder></form> </body> </HTML> 'ASP code Imports System.Data.SqlClient Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder Protected WithEvents Button1 As New System.Web.UI.WebControls.Button() #Region " Web Form Designer Generated Code " <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try PlaceHolder1.Controls.Add(Button1) Button1.Text = "Display Image" Dim nid As Integer = Convert.ToInt32(Request.QueryString("id")) Dim con As New SqlConnection("server=127.0.0.1;Trusted_Connection=yes;database=NorthWind") Dim cmd As New SqlClient.SqlCommand("SELECT Image FROM Images WHERE ID = " & nid, con) If con.State = ConnectionState.Closed Then con.Open() Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader() If dr.Read() Then Response.ContentType = "image/JPEG" Response.BinaryWrite(CType(dr("Image"), Byte())) End If If Not con.State = ConnectionState.Closed Then con.Close() If Not dr.IsClosed Then dr.Close() If Not cmd Is Nothing Then cmd.Dispose() Catch ex As Exception Response.Write(ex.ToString) End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Response.Redirect("http://localhost/TestImageFromDatabaseSQL/WebForm1.aspx?id=6") End Sub End Class Quote Visit...Bassic Software
Ev Conrad Posted January 28, 2003 Author Posted January 28, 2003 Sucks for me. I can't make the above work after cutting and pasting (after changing the query and connection string). I'm using C#, but the same concepts apply. I also tried to run it in VB - it runs, but not with the expected result. Any other ideas are appreciated. Quote
Moderators Robby Posted January 28, 2003 Moderators Posted January 28, 2003 Oh, I thought you were using VB, I think I have something in C#, I'll check... Quote Visit...Bassic Software
Ev Conrad Posted January 29, 2003 Author Posted January 29, 2003 Thanks again! I have been able to bring up the image - the only problem is that I am using a sqlDataReader item to do so, and if I attempt to give a byte buffer anything other than a size of constant value, I get an error. My code looks something like this (hopefully it formats ok): // Code that works reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess); if (reader.Read()) { imageLength = 9672; // reader[0].Length; imageData = new byte[imageLength]; Response.ClearContent(); Response.ContentType = "image/gif"; Response.AppendHeader("Content-Disposition", "filename=foo.gif"); retval = reader.GetBytes(0, 0, imageData, 0, 9672); Response.BinaryWrite(imageData); Response.Flush(); } When I attempt to do the below, it chokes: reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess); if (reader.Read()) { imageLength = reader[0].Length; imageData = new byte[imageLength]; Response.ClearContent(); Response.ContentType = "image/gif"; Response.AppendHeader("Content-Disposition", "filename=foo.gif"); retval = reader.GetBytes(0, 0, imageData, 0, imageLength); Response.BinaryWrite(imageData); Response.Flush(); } Any insight is appreciated. Everett Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.