locked
Displaying the File within a generic template RRS feed

  • Question

  • User814302705 posted

    Hi,

    When I upload the plain text file to the sql database, I can display the file as a webpage by using this url http://mickslicks.com/FileStream.ashx?FileId=6e7b5c98-94ed-4636-9a35-a4dbaba4e1ec.  I can display the FileID as a hyperlink in a gridview and when the user finds the file they want, they can click and it goes to the above link so the text file will be displayed in the browser.  The issue is that it is just a plain white page with text on it.  I would like it to be formatted to match the rest of my website, so the text file will display, BUT it would be within a generic template.  This would also be a huge time-saver because then I would not have to make a new webpage for every row in my table. 

    One thing I tried was using an Iframe within a page to display the above link and this works for the formatting.  The problem is that I still have to make a new web page for every file the user wants to see.  This can be quite time consuming with a database table that has, let's say 250,000 rows. 

    Any help is appreciated.

    Thanks,

    Micheel3000

    Tuesday, October 25, 2011 10:49 AM

All replies

  • User814302705 posted

    Hi, 
    Below is the code I am using in the webhandler page. When it returns the text filestream from the database, it is a plain white page with text on it. Is it possible to make it so that it returns as a nicely formatted webpage, with the text from the database on it?
     
     
    <%@ WebHandler Language="VB" Class="FileStream" %>
    
    Imports System
    Imports System.Web
    Imports System.Configuration
    Imports System.Data
    Imports System.Data.SqlClient
    
    ''' 
    ''' Reponsible for flushing out the file from the database to the user.
    ''' 
    Public Class FileStream : Implements IHttpHandler
        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            ' get the file id
            Dim fileId As New Guid(context.Request.QueryString("FileId"))
    		
            Dim command As New SqlCommand()
    		
            Try
                ' get the file from database
                command.Connection = New SqlConnection(ConfigurationManager.ConnectionStrings("artistalbumsong").ConnectionString)
                command.CommandText = "select * from [File] where FileId = @FileId"
                command.Parameters.Add("@FileId", SqlDbType.UniqueIdentifier).Value = fileId
                command.Connection.Open()
                Dim reader As SqlDataReader = command.ExecuteReader()
                If reader.Read() Then
                    ' flush out the binary data to the user
                    context.Response.Clear()
                    context.Response.ContentType = reader("FileType").ToString()
                    context.Response.AddHeader("Content-Disposition", String.Format("inline;filename={0};", reader("FileName").ToString()))
                    context.Response.AddHeader("Content-Length", reader("FileSize").ToString())
                    context.Response.BinaryWrite(DirectCast(reader("FileContent"), Byte()))
                    context.Response.End()
    End If
    Finally
    command.Dispose()
    End Try
    End Sub
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
    Return False
    End Get
    End Property
    End Class
    
    Saturday, November 26, 2011 5:22 PM
  • User814302705 posted

    Hi Everybody,

    The only way I could figure out how to do this was to load the filestream into an iframe embedded into the generic webpage using the query string.  It works pretty fast, but i'm still not able to tell the iframe to adjust to the size of the data.  Any help with this would be greatly appreciated.   The website is this:

    www.mickslicks.com

    Thanks,

    Micheel3000

    Thursday, July 5, 2012 3:54 AM