In my MVC application, I recently have configured a page to allow an arbitrary file type to be uploaded(with certain restrictions that dont apply to this question).
I am storing the file as data type byte[] in the database, with the stored file type based off of the file extension(Please dont try to give me a better option for storing these files, I am well aware that storing files in the database is not a good practice, but we have a constraint that requires that we persist these files using SQL Server.)
As I was saying, to make this even worse, I am storing the byte[] array of the file in a column in the database which is of type text. This is only done so I dont have to worry about restrictions with the varbinary type.
What I want to know is, when a file is requested, what is the best way in MVC to return these files to the user with a specified file extension?
I have been able to do this before with excel files and an AJAX call to a "GET" action on my controller, but I want to know if there is a better way to do it.
Any suggestions?
Example: If I have the following code
string fileExtension = /*Some File Extension*/
byte[] data = MyDataContext.DocumentTable.First(p => p.DocumentUID == id);
How can I then return this data to the user in the specified file format using the fileExtension that was originally persisted.
EDIT I am guessing that FileResult will be one of the easiest ways to accomplish this.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…