Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

change filehandler to read more than just word documents

Posted on 2007-03-26
13
Medium Priority
?
355 Views
Last Modified: 2010-05-18
how can i edit my filehandler so it reads doc,pdf, and xls
<%@ WebHandler Language="VB" Class="FileHandler" %>
 
Imports System
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
 
Public Class FileHandler
    Implements IHttpHandler
 
    Const conString As String = "Data Source=JMI-DB03;Initial Catalog=JMI;Persist Security Info=True;User ID=cholo;Password=pele@indy"
 
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "application/msword"
 
        Dim con As SqlConnection = New SqlConnection(conString)
        Dim cmd As SqlCommand = New SqlCommand("SELECT FileBytes FROM dbo.jobreferral WHERE Id=@Id", con)
        cmd.Parameters.AddWithValue("@Id", context.Request("Id"))
        Using con
            con.Open()
            Dim file() As Byte = CType(cmd.ExecuteScalar(), Byte())
            context.Response.BinaryWrite(file)
        End Using
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
 
End Class
0
Comment
Question by:gianitoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 5

Expert Comment

by:zeroxp
ID: 18797288
if you are talking about tell the client how to open pdf and xls files these are the content type you need to set to the response:
application/pdf
application/vnd.ms-excel
to decide which one to use, you may need store document type in you jobreferral DB or decide by original filename.
and of course the client need know how to open these types, otherwise that will become a download dialog.
0
 
LVL 1

Author Comment

by:gianitoo
ID: 18797309
how can i just open as a download dialog.  i will have multiple filetypes and having word only will be an issue
0
 
LVL 5

Expert Comment

by:zeroxp
ID: 18797317
add this header will force a download dialog
Response.AddHeader("content-disposition", "attachment; filename=file.txt");
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:gianitoo
ID: 18797330
do i add it instead of this?
 
        context.Response.ContentType = "application/msword"
0
 
LVL 5

Expert Comment

by:zeroxp
ID: 18797393
you can have both
0
 
LVL 1

Author Comment

by:gianitoo
ID: 18800131
i get this
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'Response' is not declared.

Source Error:

 

Line 12:  
Line 13:     Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Line 14:         Response.AddHeader("content-disposition", "attachment; filename=file.txt");
Line 15:         context.Response.ContentType = "application/msword"

 
0
 
LVL 5

Expert Comment

by:zeroxp
ID: 18804323
you need to use context.Response
0
 
LVL 1

Author Comment

by:gianitoo
ID: 18804782
where do i add context.Response
so instead of
context.Response.ContentType = "application/msword"

i add
context.Response
0
 
LVL 5

Expert Comment

by:zeroxp
ID: 18804866
context.Response.AddHeader("content-disposition", "attachment; filename=file.txt");
0
 
LVL 1

Author Comment

by:gianitoo
ID: 18826004
i get this
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30037: Character is not valid.

Source Error:

 

Line 12:  
Line 13:     Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Line 14:         context.Response.AddHeader("content-disposition", "attachment; filename=file.txt");
Line 15:         context.Response.ContentType = "application/msword"

 
0
 
LVL 5

Expert Comment

by:zeroxp
ID: 18827379
please delete the ';' at end of the line if you use VB...
0
 
LVL 1

Author Comment

by:gianitoo
ID: 18850485
zeroxp.   it works but they all open in text file.    i need to open  with their respectives programs.   if pdf  then acrobat.  if word them word  , if excel then excel program.  

do i make sense?
0
 
LVL 5

Accepted Solution

by:
zeroxp earned 2000 total points
ID: 18854890
as i previously said, you have to decide the file type from DB or some other method.
and when you write that to the client, you need to change the attachment filename accrodingly.
you need to do something similar to this:

      ' fileType is a String
      Dim encoding As New System.Text.ASCIIEncoding()
      Dim someBytes As Byte() = encoding.GetBytes("This is a test. Do not need this if bytes from DB")
      context.Response.Clear()
      Select Case fileType
         Case "TEXT"
            context.Response.AppendHeader("content-disposition", "attachment; filename=file.txt")
            context.Response.ContentType = "text/plain"
         Case "WORD"
            context.Response.AppendHeader("content-disposition", "attachment; filename=file.doc")
            context.Response.ContentType = "application/msword"
         Case "EXCEL"
            context.Response.AppendHeader("content-disposition", "attachment; filename=file.xls")
            context.Response.ContentType = "application/vnd.ms-excel"
         Case "PDF"
            context.Response.AppendHeader("content-disposition", "attachment; filename=file.pdf")
            context.Response.ContentType = "application/pdf"
         Case Else
            context.Response.AppendHeader("content-disposition", "attachment; filename=file.unknown")
            context.Response.ContentType = "application/download"
      End Select
      context.Response.BinaryWrite(someBytes)
      context.Response.End()
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question