I have a MVC 2 web application. The website captures grant applications for loans. With each application I can upload documents. The way that we upload documents to the database is as follows:
private IEnumerable<byte> GetStreamByteArrayData(Stream stream)
{
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
for (int byteIndex = 0; byteIndex < bytesRead; byteIndex++)
{
yield return buffer[byteIndex];
}
}
}
The calling method looks like this:
Convert.ToBase64String(GetStreamByteArrayData(hpf.InputStream).ToArray());
In my grid that displays the uploaded documents I have the document name, mime type and so forth. What I am trying to do is to have the name of the document in a link. When the link is clicked then the document is opened. I have no idea how to do this in an MVC app.
Can someone please advise or provide some sample source code? All help would be appreciated.
Thanks.
Assuming that you have stored the name, mime type and contents of each document into the database you could have a controller action which will serve a file given it’s unique id:
The Document model might look something like this:
And finally you could generate links to this action in your grid:
If you don’t have the content type and name of the document stored into the database you could pass them as action parameters: