(ASP.NET MVC) Files Served in the Browser Have No Extension -


i have following code serves file user in browser. served file has no extension, , users have add extension manually in order open it.

public actionresult opendoc(int id = 0) {     document doc = dbcontext.documents.single(d => d.docid == id);      byte[] data = (byte[])doc.data;     string mimetype = doc.mimetype;     return file(data, mimetype, doc.docname); } 

when debug, see mimetype "application/pdf" correct. however, file being served without pdf extension. why , how can correct it? in advance...

either:

return file(data, mimetype); 

this won't suggest file name saved file , browser suggest 1 based on uri along mapping application/pdf .pdf.

or:

return file(data, mimetype, doc.docname + ".pdf"); 

which add ".pdf" file name suggested.

of course, if there multiple mimetypes in dbcontext.documents you'll need have way of knowing extension use in given case. easiest way save extension upon file being added:

return file(data, mimetype, doc.docname + "." + doc.extension); 

alternatively can have lookup of supported filetypes, or include extension in docname begin with.


Comments