Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I am developing a web application. In this application a user can upload and download a file .pdf and stored in Sql Server as binary.I show the list of files in a gridview. When user clicks on a download button in gridview it can be downloaded the file on client side.

Can some one help me.

thanks

bandroid
Posted
Comments
Sudhir Kumar Srivastava lko 6-Sep-11 3:46am    
i think, you should save the file in root directory of your application instead of saving in Database
bandroid 6-Sep-11 4:38am    
if i save the file in root directory, can i see an example source code in vb.net ?
thanks

I do it like this: I have a table in my page, to which I add all the relevant file names from my DB.
An ASPX control then handles the download for me, based on the row index from the DB.

Page Load event handler:
C#
Dictionary<string, Guid> downloads = GetDownloadList();
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, Guid> file in downloads)
    {
    sb.Append("<tr><td>");
    sb.Append(file.Key);
    sb.Append("</td></tr>");
    HtmlTableRow row = new HtmlTableRow();
    HtmlTableCell cell = new HtmlTableCell();
    cell.InnerHtml = "<a href=\"wm5ftdl.aspx?file=" + file.Value + "\" target=\"_blank\">" + file.Key + "</a>";
    row.Cells.Add(cell);
    tbDownloads.Rows.Add(row);
    }




wm5ftdl.ASPX file:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="wm5ftdl.aspx.cs" Inherits="wm5ftdl" %>

<%
    // Send a download file to the client given the filename.
    string guid = Request.QueryString["file"];
    string fileName = "ERROR";
    byte[] data = new byte[] { 0, 0, 0, 0 };
    string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DownloadDatabase"].ConnectionString;
    using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon))
        {
        con.Open();
        string strcmd = "SELECT [iD] ,cn.[fileName],[description] ,[dataContent] ,[version] " +
                        "FROM dlContent cn " +

                        "WHERE cn.iD=@ID";
        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(strcmd, con))
            {
            cmd.Parameters.AddWithValue("@ID", guid);
            using (System.Data.SqlClient.SqlDataReader r = cmd.ExecuteReader())
                {
                if (r.Read())
                    {
                    fileName = (string) r["filename"];
                    data = (byte[]) r["dataContent"];
                    }
                }
            }
        }
    Response.Clear();
    Response.AddHeader("Cache-Control", "no-cache, must-revalidate, post-check=0, pre-check=0");
    Response.AddHeader("Pragma", "no-cache");
    Response.AddHeader("Content-Description", "File Download");
    Response.AddHeader("Content-Type", "application/force-download");
    Response.AddHeader("Content-Transfer-Encoding", "binary\n");
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
    Response.BinaryWrite(data);
    //Response.WriteFile("wm5ftdata/" + fileName);
    Response.End();
%>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900