如果有人将任何文件(excel 除外)的扩展名重命名为 xls 和 xlsx(请不要问我为什么:-( ),我需要检查其有效性(如果这仍然是有效的 excel 文件)。我正在使用 mime类型和它不工作。我错过了什么吗?
const string excel2007MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
const string excel2003MimeType = "application/vnd.ms-excel";
string excelExtention = string.Empty;
excelExtention = System.IO.Path.GetExtension(myFilePath.PostedFile.FileName).ToLower();
string mimeType = myFilePath.PostedFile.ContentType;
Response.Write("mime type" + mimeType + Environment.NewLine);
if(
(
!(excelExtention == ".xls" && mimeType == excel2003MimeType)
||
!(excelExtention == ".xlsx" && mimeType == excel2007MimeType)
)
)
{
Response.Write ("only excel file is permitted");
}
我将 jpg 文件重命名为 xlsx 文件并上传。如果我打印出变量 mimetype,其值为“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”。我不知道为什么,因为内容不是 Excel 文件。它是一个图像。
为此,您需要尝试使用 Excel 库或 Excel COM 对象本身打开它。遗憾的是,Microsoft 不支持服务器环境中的 Office COM 对象自动化。
您可以只读取文件的第一部分并检查二进制签名,但支持所有可能的 XLS 版本将需要大量工作; .XSLX 文件只是包含多个部分的文档的 ZIP 文件。
希望有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)