[RESOLVED]Serving files in Database as hyperlinks

Hey everyone,

     I’m a newcomer that’s learning ASP.Net Web by doing simple hobby projects I find on the web. Recently I came across "Save And Retrieve
Files From a Sql Server CE Database with WebMatrix
" and  I was wondering if I could get advice on how to adapt the download code provided. Currently it displays the results of the request on the page that contains the FileUpload helper but I would like
to display on a page that houses all the DB data in a grind view and that each row has its own link for the user.

Example:

FileName                          FileDate   FileDownload

Story of the Yellow Dog   09/20/14    "Download"

I’ve been playing around with the code but after a full day, its clear that the solution to this is outside my current level and searches only yield answers that use ASP. Net C# and VB.Net to code the functions and Download link.

You have all the ingredients in the download that comes with that article. All you need to do is to query the database for the correct fields and pas the result to a WebGrid :

var sql = "Select * From Files";
var files = db.Query(sql);
var grid = new WebGrid(files);

Then you need to provide a download link in one of the columns. You do this via the "format" parameter:

@grid.GetHtml(
           columns: grid.Columns(
               grid.Column("FileName", "File"),
               grid.Column("FileId", "FileDownLoad", format:@<a href="download.cshtml?Id=@item.FileId">Download</a>)))

There’s loads of articles on my site that explain how to use the WebGrid: http://www.mikesdotnetting.com/Category/31

Thank you very much Mike and I apologize for not responding sooner, University work load is currently in high gear. I had a feeling that the key to displaying the information how I wanted had to do with the location of the:

<a href="download.cshtml?Id=@item.FileId">

and I spent and entire night trying to "force" it into my web grid in various way that ended breaking the site. I even tried reviewing your "ASP.NET MVC Uploading and Downloading Files" article to see if there was something there I could adapt to my needs
and talk about "looking at Calculus to understand Algebra", haha.

Thanks for demonstrating the path I should be on and linking to me more helpful information(your site is a great tool for beginners like myself). Will be making good use of it tonight.

LearningRobot

Thank you very much Mike and I apologize for not responding sooner, University work load is currently in high gear. I had a feeling that the key to displaying the information how I wanted had to do with the location of the:

<a href="download.cshtml?Id=@item.FileId">

and I spent and entire night trying to "force" it into my web grid in various way that ended breaking the site. I even tried reviewing your "ASP.NET MVC Uploading and Downloading Files" article to see if there was something there I could adapt to my needs
and talk about "looking at Calculus to understand Algebra", haha.

Thanks for demonstrating the path I should be on and linking to me more helpful information(your site is a great tool for beginners like myself). Will be making good use of it tonight.

Hey there,

Unsure if this helps – Mike’s code is definitely the correct way to do it.. This code might help later on if you want to do something similar without a table..

@{
       var db= Database.Open("Database");
       var sqlQ = "SELECT * FROM Files";
       var data = db.Query(sqlQ);
     }      
       <ol>

         @foreach(var row in data){
           <li><a href="download.cshtml?Id=@item.FileId?id=@row.id">@row.FileName</a></li>
         }

       </ol>   

Handy little snippet – I use it a fair bit in my projects :)

Leave a Reply