我目前正在使用C#和Asp.Net MVC的Web应用程序。在其中一页上,我有一个要求,即用户可以下载文件并填写相关数据,然后上传。由于一些使用旧机器的用户,我正在使用.xls
和.xlsx
。可以下载哪个文件是基于用户必须选择的下拉值。使用相同的方法来下载不同的文件
我有两个按钮一个用于.xls
和一个用于xlsx
文件。我的问题是如何使用相同的后端代码在文件之间进行交换。因此,如果单击.xls
,用户将获得.xls
文件,如果另一个单击,则它们会收到.xlsx
文件。
这是我到目前为止的代码:
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 I swap between the .xls and .xlsx file?
break;
case "Policy":
templateName = "Policy Xls File"; //How can I swap between the .xls and .xlsx file?
break;
case "Consignment":
templateName = "Consignment Xls File"; //How can I swap between the .xls and .xlsx file?
break;
case "Quality":
templateName = "Quality Xls file"; //How can I swap between the .xls and .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);
}
该方法如何知道它是否是XLS或XLSX?您似乎没有为此传递参数,也没有“发件人”来查看哪个按钮被点击。为什么不在这两种情况下都提供XLS,因为新版本的Excel打开此文件。你为什么需要两个? – Equalsk