Allow users to Upload (using FIleUpload Control) and retrieve various MIME types to a SQL Server 2008 database and store MIME values in a VarBinary(MAX) Field

****Very Important .Net 3.5 utilizing VB.Net Syntax***

Allow users to Upload (using FIleUpload Control)  and retrieve various MIME types to a SQL Server 2008 database and store MIME values in a VarBinary(MAX) Field

I have two pages. The first page submits parameters to the second page. The second page only calls the code to allow a user to download the allowed submitted MIME types to the database stored in VarBinary(MAX). PDF, Word, Excel, JPG. This solution works very well and has not failed once in testing for two days of pounding on the system. The big question at my job now is what is needed to modify the code (if it can be modified) to allow the word document or other MIME types to display directly in the browser? The orginal business requirement was to allow any of the MIME types to display directly in the browser but it was giving me so much trouble I had to back off. I do believe that someone on here may be able to help. It would need to be a VB.Net solution, not C#. I would need the true code markup because admittedly I am not the best at code writing. PLEASE HELP!!!

PAGE 1 - PASS THE QUERYSTRING - ASPX
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="doclocate.aspx.vb" Inherits="doclocate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            height: 19px;
        }
        .style3
        {
            width: 1000px;
            height: 65px;
        }
        .style4
        {
            font-family: Arial;
            font-size: small;
            font-weight: bold;
        }
        .style5
        {
            font-family: Arial;
            font-size: 10pt;
            color: #5DBA35;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table cellpadding="0" cellspacing="0" class="style1">
            <tr>
                <td>
                    <img alt="" class="style3" src="logo/toplogo.jpg" /></td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style4">
                    Download Documents Submitted with Requirements</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style5">
                    Search by Request ID</td>
            </tr>
            <tr>
                <td>
                    <asp:TextBox ID="rid" runat="server"></asp:TextBox>
                    <asp:Button ID="Button1" runat="server" onclientclick="documentbyrid"
                        Text="Button" />
                </td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
                        DataKeyNames="rid" DataSourceID="documentbyrid"
                        style="font-family: Arial; font-size: 8pt" Width="800px">
                        <Columns>
                            <asp:BoundField DataField="rid" HeaderText="rid" ReadOnly="True"
                                SortExpression="rid" />
                            <asp:BoundField DataField="a_id" HeaderText="a_id" SortExpression="a_id" />
                            <asp:BoundField DataField="Status" HeaderText="Status"
                                SortExpression="Status" />
                            <asp:BoundField DataField="dateneeded" HeaderText="dateneeded"
                                SortExpression="dateneeded" />
                            <asp:BoundField DataField="reportName" HeaderText="reportName"
                                SortExpression="reportName" />
                            <asp:BoundField DataField="existinginfo" HeaderText="existinginfo"
                                SortExpression="existinginfo" />
                            <asp:BoundField DataField="reportDescription" HeaderText="reportDescription"
                                SortExpression="reportDescription" />
                            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style5">
                    Complete List of unfinished Request ID with attached support documents</td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
                        AutoGenerateColumns="False" DataKeyNames="rid" DataSourceID="showdocs_all"
                        style="font-family: Arial; font-size: 8pt" Width="800px">
                        <Columns>
                            <asp:BoundField DataField="rid" HeaderText="rid" ReadOnly="True"
                                SortExpression="rid" />
                            <asp:BoundField DataField="a_id" HeaderText="a_id" SortExpression="a_id" />
                            <asp:BoundField DataField="Status" HeaderText="Status"
                                SortExpression="Status" />
                            <asp:BoundField DataField="dateneeded" HeaderText="dateneeded"
                                SortExpression="dateneeded" />
                            <asp:BoundField DataField="reportName" HeaderText="reportName"
                                SortExpression="reportName" />
                            <asp:BoundField DataField="existinginfo" HeaderText="existinginfo"
                                SortExpression="existinginfo" />
                            <asp:BoundField DataField="reportDescription" HeaderText="reportDescription"
                                SortExpression="reportDescription" />
                            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                            <asp:BoundField DataField="idvalue" HeaderText="idvalue"
                                SortExpression="idvalue" />
                            <asp:HyperLinkField DataNavigateUrlFields="rid,idvalue"
                                DataNavigateUrlFormatString="getdocument.aspx?rid={0}&idvalue={1}"
                                Text="Download Document" />
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:SqlDataSource ID="showdocs_all" runat="server"
                        ConnectionString="<%$ ConnectionStrings:reportfeedRequirementsConnectionString %>"
                        SelectCommand="SELECT * FROM [viw_getreqdocs]"></asp:SqlDataSource>
                    <asp:SqlDataSource ID="documentbyrid" runat="server"
                        ConnectionString="<%$ ConnectionStrings:reportfeedRequirementsConnectionString %>"
                        SelectCommand="SELECT * FROM [viw_getreqdocs] WHERE ([rid] = @rid)">
                        <SelectParameters>
                            <asp:FormParameter FormField="rid" Name="rid" Type="Int32" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

PAGE 2 - RECEIVE THE QUERYSTRING PARAMETERS AND ALLOW DOWNLOADING OF DOCUMENTS ASPX.VB
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Partial Class getdocument

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim rid As Int32
        Dim idvalue As Int32
        Dim strQuery As String = "select Name, ContentType, Data from requestdocs where rid=@rid and idvalue = @idvalue"
        Dim cmd As SqlCommand = New SqlCommand(strQuery)
        rid = Convert.ToInt32(Request.QueryString("rid"))
        idvalue = Convert.ToInt32(Request.QueryString("idvalue"))

        cmd.Parameters.Add("@rid", SqlDbType.Int).Value = rid
        cmd.Parameters.Add("@idvalue", SqlDbType.Int).Value = idvalue
        Dim dt As DataTable = GetData(cmd)
        If dt IsNot Nothing Then
            download(dt)
        End If
    End Sub




    Public Function GetData(ByVal cmd As SqlCommand) As DataTable
        Dim dt As New DataTable
        Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("reportfeedRequirementsConnectionString").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim sda As New SqlDataAdapter
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Try
            con.Open()
            sda.SelectCommand = cmd
            sda.Fill(dt)
            Return dt
        Catch ex As Exception
            Response.Write(ex.Message)
            Return Nothing
        Finally
            con.Close()
            sda.Dispose()
            con.Dispose()
        End Try
    End Function




    Protected Sub download(ByVal dt As DataTable)
        Dim bytes() As Byte = CType(dt.Rows(0)("Data"), Byte())
        Response.Buffer = True
        Response.Charset = ""
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.ContentType = dt.Rows(0)("ContentType").ToString()
        Response.AddHeader("content-disposition", "attachment;filename=" & dt.Rows(0)("Name").ToString())
        Response.BinaryWrite(bytes)
        Response.Flush()
        Response.End()
    End Sub

   

End Class
derrekdeveloperAsked:
Who is Participating?
 
cyberkiwiCommented:
There is (or was) an Internet Explorer setting to control that, by letting IE host Office apps.  I don't believe any other browser is able to do that.  PDFs can be inlined because Adobe provides the add-ins to the browsers to host it.

For IE, the _reverse_ of the link in 32272051 may work.
0
 
cyberkiwiCommented:
>>The big question at my job now is what is needed to modify the code (if it can be modified) to allow the word document or other MIME types to display directly in the browser?

Do you mean that the documents are currently forcing a "save to" instead of showing in the browser window itself?

Change this line near the bottom to "inline":

        Response.AddHeader("content-disposition", "attachment;filename=" & dt.Rows(0)("Name").ToString())

        Response.AddHeader("content-disposition", "inline;filename=" & dt.Rows(0)("Name").ToString())
0
 
derrekdeveloperAuthor Commented:
You are correct Cyberkiwi. You can open or save the document with the respective application like MS Word. However, the orginal requirement was for the information to open in a browser. I will make the change and implement your new code tomorrow when I get to work.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
sybeCommented:
> what is needed to modify the code (if it can be modified) to allow the word document or other MIME types to display directly in the browser?

It is a browser setting, you can not change that from the server.
0
 
derrekdeveloperAuthor Commented:
Cyberkiwi, I changed the code as suggested. It works well for PDF file type. The PDF opens and displays in the browser. However, the .doc and .docx (MS Word) offers the option to Open or Download the document.
0
 
derrekdeveloperAuthor Commented:
You can change the setting in Internet Explorer as suggested.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.