我所做的是用于文件上传的ajax表单帖子,但它似乎是重定向整个表单.Below是控制器操作方法和部分视图。
public JsonResult FileUpload()
{
string message = "File is empty or null";
JsonResult jsonResult=null;
foreach (string upload in Request.Files)
{
if (!Request.Files[upload].HasFile()) continue;
string path = AppDomain.CurrentDomain.BaseDirectory + "/Attachment/Audit/";
string filename = Path.GetFileName(Request.Files[upload].FileName);
Request.Files[upload].SaveAs(Path.Combine(path, filename));
message = "File Uploaded Successfully";
}
jsonResult = Json(message, JsonRequestBehavior.AllowGet);
return jsonResult;
}
部分视图
@using XXXX.Models
@using XXXX.ViewModels
@using XXXX.UtilityModels
@model AuditModel
<div id="pvView" class="view">
<div id="tabStripBottom" data-role="tabStripBottom" class="tabStripBottom k-state- active">
.
.
.
.
<div>
@using (Html.BeginForm("FileUpload", "Audit", FormMethod.Post, new { enctype = "multipart/form-data", id = "ajaxUploadForm" }))
{
<input type="file" name="FileUpload1" /><br />
<input type="submit" name="Submit" id="Submit" value="Upload" />
}
</div>
<span>
<label id="lblMessg"></label>
</span>
</div>
<input type="button" value="Save" onclick="onSave" />
</div>
<script>
$(function () {
$("#ajaxUploadForm").ajaxForm({
iframe: true,
type: 'POST',
dataType: "json",
cache: false,
timeout: 1200000,
async: true,
beforeSubmit: function () {
},
success: function (result) {
debugger;
$("#lblMessg").html() = result.message;
alert("sucess");
},
error: function (xhr, textStatus, errorThrown) {
alert("Error uploading file");
}
});
});
.
.
.
.
</script>
任何人都可以帮助我吗? 提前致谢