Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to publish a document stored in SQL using the original file name as a valid Url?

I have a database that stores PDF documents as a byte stream in a varbinary(max) column, The original document name is stored in a varchar column with the document extension and the primary key is a guid. (This schema is fixed, documents must reside in the database)

There are no problems retrieving the document as a stream and either writing to a temporary file or streaming directly to a web browser.

My question is how could the document also be exposed via a Url using it's original file name?

An good example of what I am trying to do is SharePoint, Under the covers SharePoint stores the files inside the SQL database and exposes the document as a Url (With original file extension) that can be directly opened with a browser.

eg: http://WebServer/Documents/MyDocument.pdf

(The platform is SQL 2008, IIS 7.5, .NET 4, C#)

like image 853
Nigel Avatar asked Feb 25 '23 00:02

Nigel


1 Answers

If your site uses ASP.Net MVC you can just add controller for /Documents download file by name:

routes.MapRoute(
            "GetFiles",
            "Documents/{fileName}",
            new
            {
                controller = "Files",
                action="GetFile",
            }
        );

And controller class:

    public class FilesController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult GetFile(string fileName)
    {...
    }
}
like image 63
Alexei Levenkov Avatar answered Feb 27 '23 14:02

Alexei Levenkov