Click here to Skip to main content
15,894,540 members
Home / Discussions / C#
   

C#

 
QuestionConverting textbox into textarea. Pin
Diego Carrion25-Aug-12 16:16
Diego Carrion25-Aug-12 16:16 
AnswerRe: Converting textbox into textarea. Pin
OriginalGriff25-Aug-12 22:26
mveOriginalGriff25-Aug-12 22:26 
GeneralRe: Converting textbox into textarea. Pin
Diego Carrion25-Aug-12 22:36
Diego Carrion25-Aug-12 22:36 
GeneralRe: Converting textbox into textarea. Pin
BillWoodruff26-Aug-12 15:36
professionalBillWoodruff26-Aug-12 15:36 
GeneralRe: Converting textbox into textarea. Pin
Diego Carrion27-Aug-12 16:31
Diego Carrion27-Aug-12 16:31 
QuestionStoring File Path on Database Pin
ASPnoob25-Aug-12 14:34
ASPnoob25-Aug-12 14:34 
AnswerRe: Storing File Path on Database Pin
AmitGajjar25-Aug-12 22:20
professionalAmitGajjar25-Aug-12 22:20 
AnswerRe: Storing File Path on Database Pin
OriginalGriff25-Aug-12 22:34
mveOriginalGriff25-Aug-12 22:34 
That is not quite what you want to do - if you are replacing the file name with a Guid, (which is very sensible) then any download of that file direct from an href link will have the GUID as the file name, rather than the original.

What I do is to provide a href link to an ASPX page that reads the file, and sends it as the original file name. (This code reads teh whole file from the database, but that is easy for you to change (mine is probably a bit more complex that yours, as it supports versions as well):
C#
<%@ 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.End();
%>


The link is generated from the CS code for the displayed page:
C#
/// <summary>
/// On page load, fill out the Download table
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
    {
    try
        {
        List<Downloadable> downloads = GetDownloadList();
        foreach (Downloadable dl in downloads)
            {
            HtmlTableRow row = new HtmlTableRow();
            HtmlTableCell cell = new HtmlTableCell();
            cell.InnerHtml = "<a href=\"wm5ftdl.aspx?file=" + dl.Id + "\" target=\"_blank\">" + dl.FileName + "</a>";
            row.Cells.Add(cell);
            tbDownloads.Rows.Add(row);
            }
        }
    catch (Exception ex)
        {
            HtmlTableRow row = new HtmlTableRow();
            HtmlTableCell cell = new HtmlTableCell();
            cell.InnerHtml = ex.ToString();
            row.Cells.Add(cell);
            tbDownloads.Rows.Add(row);

        }
    }
/// <summary>
/// Returns the list of available files, and their indexes.
/// Note that this only lists the Latest version: not all files and versions.
/// </summary>
/// <returns></returns>
private List<Downloadable> GetDownloadList()
    {
    List<Downloadable> list = new List<Downloadable>();
    string strCon = ConnectionStrings.Download;
    using (SqlConnection con = new SqlConnection(strCon))
        {
        con.Open();
        // Get only the file Ids with the highest version number.
        string strcmd = "SELECT [iD] ,cn.[fileName],[description] ,[version], [uploadedOn], [uploadedBy], [downloadUserOnly] " +
                        "FROM dlContent cn " +
                        "INNER JOIN " +
                        "( SELECT filename, MAX(Version) AS maxver " +
                        "  FROM dlContent " +
                        "  GROUP BY filename" +
                        ") gcn ON cn.filename = gcn.filename " +
                        "         AND cn.version = gcn.maxver";
        using (SqlDataAdapter da = new SqlDataAdapter(strcmd, con))
            {
            DataTable dt = new DataTable();
            da.Fill(dt);
            foreach (DataRow dr in dt.Rows)
                {
                list.Add(new Downloadable(dr));
                }
            }
        }
    return list;
    }

Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

QuestionRe: Storing File Path on Database Pin
Eddy Vluggen25-Aug-12 23:34
professionalEddy Vluggen25-Aug-12 23:34 
AnswerRe: Storing File Path on Database Pin
ASPnoob26-Aug-12 0:27
ASPnoob26-Aug-12 0:27 
GeneralRe: Storing File Path on Database Pin
Eddy Vluggen26-Aug-12 0:40
professionalEddy Vluggen26-Aug-12 0:40 
QuestionChange Combobox Look in WPF Pin
a.fatemeh25-Aug-12 9:24
a.fatemeh25-Aug-12 9:24 
AnswerRe: Change Combobox Look in WPF Pin
Abhinav S25-Aug-12 18:31
Abhinav S25-Aug-12 18:31 
QuestionIndexers Pin
ripples24-Aug-12 23:59
ripples24-Aug-12 23:59 
AnswerRe: Indexers Pin
Wes Aday25-Aug-12 0:12
professionalWes Aday25-Aug-12 0:12 
AnswerRe: Indexers Pin
DaveyM6925-Aug-12 0:37
professionalDaveyM6925-Aug-12 0:37 
AnswerRe: Indexers Pin
PIEBALDconsult25-Aug-12 4:40
mvePIEBALDconsult25-Aug-12 4:40 
AnswerRe: Indexers Pin
Richard MacCutchan25-Aug-12 7:07
mveRichard MacCutchan25-Aug-12 7:07 
GeneralRe: Indexers Pin
ripples25-Aug-12 8:52
ripples25-Aug-12 8:52 
Questioncan this be done without using 'dynamic: build a list of KeyValuePairs, where each item's Type varies ? Pin
BillWoodruff24-Aug-12 20:31
professionalBillWoodruff24-Aug-12 20:31 
AnswerRe: can this be done without using 'dynamic: build a list of KeyValuePairs, where each item's Type varies ? Pin
DaveyM6925-Aug-12 0:13
professionalDaveyM6925-Aug-12 0:13 
GeneralRe: can this be done without using 'dynamic: build a list of KeyValuePairs, where each item's Type varies ? Pin
BillWoodruff25-Aug-12 11:25
professionalBillWoodruff25-Aug-12 11:25 
AnswerRe: could this be done without dynamic Pin
Martijn Kok25-Aug-12 0:59
Martijn Kok25-Aug-12 0:59 
GeneralRe: could this be done without dynamic Pin
BillWoodruff25-Aug-12 11:33
professionalBillWoodruff25-Aug-12 11:33 
Questionasp.net Pin
Abubakar Shaikh .24-Aug-12 20:05
Abubakar Shaikh .24-Aug-12 20:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.