【发布时间】:2013-05-21 14:17:47
【问题描述】:
我的项目中有一个upload.ascx。它将在 Jquery Popup 中加载。
upload.ascx 包含 File Upload 控件。文件上传控件将上传.xlsx and .xls 文件。我添加了Java script 函数来执行此验证(以防止上传不必要的文件)。
Onchange of FileUpload 控制和Onclick of Submit button 将调用相同的验证函数。
文件上传验证功能对这两个调用都有效。如果用户单击提交按钮,则会调用相同的函数并正常工作。
我的问题是:在我的验证函数中我写了return false。显示警报消息后,页面被重定向到某个 URL (localhost/Admin/Authorization/AcceptFile.aspx)。 AcceptFile 是我的 ActionResult 名称。它不应该发生。函数返回 False。但是表单被重定向到上面的 URL 为什么。?
我在我的操作结果中保留了调试器,如果它的文件错误(它是正确的),它不会被调用。如果它的文件正确,索引文件将加载成功消息(操作结果被调用并且工作正常)。 .
我怀疑 MVC 表单。如果上传了错误的文件,则在没有调用 Action Result 的情况下发生重定向,这应该停止。
<% using (Html.BeginForm("AcceptFile", "Authorization", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
我的 Javascript 函数:
function checkfile(sender) {
var validExts = new Array(".xlsx", ".xls");
var fileExt = sender.value;
var strValue = false;
fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
if (validExts.indexOf(fileExt) < 0) {
ShowMessage(-1, "Invalid file selected, valid files are .xlsx and .xls types.");
strValue = false;
}
else {
strValue = true;
}
return strValue;
}
我的 upload.ascx 代码:
<div>
<% using (Html.BeginForm("AcceptFile", "Authorization", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<input type="file" id="fileAuthorization" name="fileAuthorization" onchange="checkfile(this);" />
<input type="submit" id="btnSave" name="btnSave" value="Upload" onclick="javascript:return checkfile(document.getElementById('fileAuthorization'))" />
<%} %>
</div>
【问题讨论】:
标签: javascript asp.net-mvc form-submit