Solved

Open File in Correct Application - .NET / Blob / Oracle / Datagrid

Posted on 2004-04-23
7
227 Views
Last Modified: 2007-12-19
I was wondering if someone an help me get this solution.

I insert a blob into our oracle 9i database.  That's fine, no problems with the insert.  Now I want to return the information (file name and blob) and place it into a datagrid.  All I want though is for the user to see a list of file names and they can click on a file name and it will open the file up in the correct application (word, excel etc...)

Here is some of what I have.  This code is what I'm using to manipulate the datagrid, but it's really wrong.  I'm returning  two field names from my stored procedure: cmnt_txt (name of the blob file) and the actual blob file.

**************************************************************
 Private Sub dgappln_attm_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgappln_attm.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim HLAttachment As HyperLink = e.Item.Cells(0).FindControl("HLAttachment")
            HLAttachment.NavigateUrl = e.Item.DataItem("cmnt_txt")  'not right.  I want it to open not navigate to a url
            HLAttachment.Text = e.Item.DataItem("cmnt_txt").ToString  'this is the list of file names
           
                  End If
    End Sub

********************************************

I'll award extra points for quick response and easy to implement.  Please be gentle, I'm a beginner and get lost easy.  :)
Thanks! ;)
0
Comment
Question by:numten
  • 3
  • 2
7 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 10908026
Step 1: Write out blob to temp file with same extension as filename you have in database (or write it to same filename etc)

Step 2: use Process.Start(Filename) (available in System.Diagnostics)

You can give it the name of a document (write out your blob to a temp file with the proper extension) and it will try to find the associated application on that machine to run.

.NET Framework Class Library  

Process.Start Method (String)
Starts a process resource by specifying the name of a document or application file and associates the resource with a new Process component.

[Visual Basic]
Overloads Public Shared Function Start( _
   ByVal fileName As String _
) As Process
[C#]
public static Process Start(
   string fileName
);
[C++]
public: static Process* Start(
   String* fileName
);
[JScript]
public static function Start(
   fileName : String
) : Process;
Parameters
fileName
The name of a document or application file to run in the process.
0
 
LVL 7

Expert Comment

by:culshaja
ID: 10916339
You need to use a hyperlink rather than a a linkbutton for the link to open the file in the datagrid. You then need to implement a HTTPModule. The following is an example:

Public Class DocumentRequestHandler
    Implements IHttpHandler


    'Notice ProcessRequest is the only method
    'exposed by the IHttpHandler
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
        Dim strFilename As String

            Dim appsrv As New WebService.ASMXFile
            'grab filename from end of URL
            Dim strArray As String() = Split(context.Request.RawUrl, "/")

            strFilename = strArray(UBound(strArray))
            strArray.Initialize()
            strArray = Split(strFilename, ".")
            strFilename = strArray(0)
            strArray = Nothing

           context.Response.ContentType = "application/pdf"
           context.Response.BinaryWrite(appsrv.GetDocumentBinary(strFilename))
           
    End Sub

    'By calling IsReusable, an HTTP factory can query a handler to
    'determine whether the same instance can be used to service
    'multiple requests

    Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

End Class

The ContentType will need to be set with the correct MIMEType based on the file extension of the file. You can simply do that with a case statement.

You need to andd the following to the <HTTPHandlers> section of Web.config:

<add verb="*" path="*xdocuments/*" type="YourProjectName.DocumentRequestHandler, YourProjectName"/>

The url of the document needs to have xdocuments/filenamefromdb for this to work as the HTTPHandler is only invoked to pull the document from the database when it sees xdocuments in the URL.

James :-)


0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10918213
oh whoops didnt see that it was asp.net. actually all you need to do is set the content type and the write the binary to response.

ill post an example when I get to the office in about 45 minutes.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:numten
ID: 10918250
I would greatly appreciate that!  :)  Thanks!
0
 

Author Comment

by:numten
ID: 10952191
I haven't had a chance to try out your suggestion, but I will tomorrow.  I did not forget about you and your hard work!
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10954524

put something along the lines of this in the page load of "File.aspx"

 //get the image id from the url
string ImageId = Request.QueryString["FileId"];

//build our query statement
string sqlText = "SELECT data, contenttype FROM files WHERE pkFileId = " + FileId;

SqlConnection connection = new SqlConnection(ConenctionString);
SqlCommand command = new SqlCommand( sqlText, connection);

//open the database and get a datareader
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if ( dr.Read()) //yup we found our image
{
Response.ContentType = dr["contenttype"].ToString();
Response.BinaryWrite( (byte[]) dr["data"] );
}
connection.Close();

then you can put your links to "file.aspx?FileId=5" (or whatever)

You can do this most of the time with your response (just change the content type). If the remote pc knows the content type it can open.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now