Javascript 如何验证正确的格式文件

Javascript 如何验证正确的格式文件,javascript,asp.net-mvc-4,razor,Javascript,Asp.net Mvc 4,Razor,我上传的Excel文件,我想当我点击上传按钮,如果它的Excel文件显示消息“文件上传成功”,否则它的不正确的文件,如img。文档等显示消息“您必须选择Excel格式文件”请帮助我如何在AJAX上执行此操作 这是我的控制器,上传成功 //Post [HttpPost,ActionName(“电子邮件加载”)] 公共操作结果电子邮件加载确认(HttpPostedFileBase文件) { File_Model tz=新文件_Model(); 尝试 { 如果(文件!=null) { SaveAs(

我上传的Excel文件,我想当我点击上传按钮,如果它的Excel文件显示消息“文件上传成功”,否则它的不正确的文件,如img。文档等显示消息“您必须选择Excel格式文件”请帮助我如何在AJAX上执行此操作

这是我的控制器,上传成功

//Post
[HttpPost,ActionName(“电子邮件加载”)]
公共操作结果电子邮件加载确认(HttpPostedFileBase文件)
{
File_Model tz=新文件_Model();
尝试
{
如果(文件!=null)
{
SaveAs(Server.MapPath(“~/App_Data/Uploads/Contacts.xls”);
_存储区存储区=新建存储区();
Basic_Helper_Basic_Helper=新的Basic_Helper();
var Store=Stores.Get\u Store\u Info\u Prd(\u Basic\u Helper.Format\u URL(Request.URL.Host.ToString());
如果(存储!=null)
{
_Basic_Helper=null;
_站点信息站点信息=新建站点信息();
string connectionString=“Provider=Microsoft.Jet.OleDb.4.0;数据源=“+Server.MapPath”(~/App_Data/Uploads/Contacts.xls”)+”;
connectionString+=“扩展属性=Excel 8.0;”;
//请务必阅读表格1。
OleDbCommand myCommand=新的OleDbCommand(“从[Sheet1$];”中选择*”;
OLEDB连接myConnection=新的OLEDB连接(connectionString);
myConnection.Open();
myCommand.Connection=myConnection;
OleDbDataReader myReader=myCommand.ExecuteReader();
while(myReader.Read())
{
如果(!myReader.GetValue(1).ToString()等于(“”)
{
站点信息。插入客户信息通讯(Store.ID,myReader.GetValue(0.ToString(),myReader.GetValue(1.ToString());
}
}
myReader.Close();
myCommand.Cancel();
myConnection.Close();
myConnection=null;
站点信息=null;
}
Store=null;
存储=空;
System.IO.File.Delete(Server.MapPath(“~/App\u Data/Uploads/Contacts.xls”);
}
}
捕获(例外情况除外)
{
字符串s=ex.ToString();
s=“”;
//Response.Write(例如ToString());
}
返回视图();

}
下面是一个通过Ajax上传文件的示例,其中包括检查Excel文件。这在IE9或更低版本中不起作用。如果您需要使用较旧的浏览器上载,我建议您使用以下插件:

客户端/浏览器:

<script type="text/javascript">
    $(function() {
        var fileUpload = function() {
            var file = document.getElementById("file").files[0];

            var xhr = new window.XMLHttpRequest();

            xhr.onload = function () {
                if (this.status === 200) {
                    var data = JSON.parse(this.responseText);

                    // do stuff...
                } else {
                    alert("Upload Failed. Your request was not completed successfully; reason: " + this.statusText);
                }
            };
            xhr.onerror = function () {
                alert("A network related error occurred when trying to process this request. Please check your connection and try again.");
            };

            xhr.open("POST", "/Home/UploadExcel", true);
            xhr.setRequestHeader("Content-Type", "multipart/form-data");
            xhr.send(file);
        };

        $("#load").on("click", function () {
            var file = document.getElementById("file").files[0];
            var fileName = file.name;
            var fileExtension = fileName.substr((fileName.lastIndexOf('.') + 1));

            if (fileExtension === "xls" || fileExtension === "xlsx") {
                fileUpload();
            } else {
                alert("Must be Excel file");
            }
        });
    });
</script>
[HttpPost]
public ActionResult UploadExcel()
{
    var error = true;
    var errorMessage = String.Empty;

    try
    {
        if (Request.InputStream != null)
        {
            //do stuff...
            error = false;
        }
        else
        {
            errorMessage = "File was null";
        }
    }
    catch (Exception x)
    {
        errorMessage = "Problem in uploading excel file; reason: " + x.Message;
    }

    var dto = new
    {
        error,
        errorMessage
    };

    return Json(dto);
}