Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

Busy developing a document server - storing files on server varbinary format and was wondering if a download link could be created similar to Dropbox, Onedrive ... for the end user to be able receive link via email - this may be a pipe dream as sever not cloud based - but just maybe its possible?

Best Regards
Richard

What I have tried:

Google Search - no related topics
Posted
Updated 7-Jul-20 3:13am
Comments
F-ES Sitecore 7-Jul-20 8:56am    
Yes it's possible. If you want user-specific or temporary links then have a table with all valid link paths and the ID of the file it is for. When you create a link create a random key for it (or use a GUID), store that in your table and when someone uses it look up the ID of the file it is for and download it. You'll need a clean-up task to clear the links out after a certain age. If you don't want temporary links then your download system must have a unique url for each file so just send them that.
Grid-Code 7-Jul-20 9:20am    
Good Day

Appreciated much indeed, shall review your code. Fortunately complete Database runs off Guid's so all good so far.

Richard
CHill60 7-Jul-20 10:16am    
For the member to be notified of your response you need to use the "Reply" link next to their post

1 solution

F-ES Sitecore is right, it's possible.
This is the code I use:
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.WriteFile("wm5ftdata/" + fileName);
    Response.End();  
%>
It goes in an ASPX file, and I provide a GUID id link:
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;
    }
This prevents the need for direct access to the DB with a modicum of security.
 
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