c# - Using same method to download different file -
i'm working on web app i'm using c# , asp.net mvc. on 1 of page have requirement users able download files , fill in relevant data upload them. due users having old machine i'm working .xls
, .xlsx
. file can downloaded based upon dropdown value user must select.
i have 2 buttons 1 .xls
, 1 xlsx
file. question how can use same backend code swap between files. if .xls
clicked user gets .xls
file , if other click receive .xlsx
file.
this code far:
public fileresult downloadtemplates(string policytype) { string templatename = string.empty; string basedirectory = "base path"; string templatedirectory = "temnplate directory path"; switch (policytype) { case "administrative": templatename = "admin xls file"; //how can swap between .xls , .xlsx file? break; case "policy": templatename = "policy xls file"; //how can swap between .xls , .xlsx file? break; case "consignment": templatename = "consignment xls file"; //how can swap between .xls , .xlsx file? break; case "quality": templatename = "quality xls file"; //how can swap between .xls , .xlsx file? break; default: templatename = string.empty; break; } string filepath = path.combine(basedirectory, templatedirectory, templatename); byte[] filedata = system.io.file.readallbytes(filepath); string contenttype = mimemapping.getmimemapping(filepath); return file(filedata, contenttype); }
there's path
method - path.changeextension
change extension you:
if path has no extension, , extension not null, returned path string contains extension appended end of path.
as work if stored name of file 1 of extensions in place (e.g. xlsx) you'd need be:
if (xlsselected) path.changeextension(filepath, ".xlsx");
obviously you'll need pass in (or otherwise determine) xlsselected
.
alternatively, if store template name without extension can do:
if (xlsselected) templatename = templatename + ".xls"; else templatename = templatename + ".xlsx";
you can go further making extension strings resources and/or configurable in case there's need change them again in future.
Comments
Post a Comment