我在 div FileUploadContainer 中使用 javascript 动态创建了多个文件上传器。当我将 btnSubmit 放在表单标签之外时,我得到 Request.Files.Count 为零。我需要通过 Json 调用 PopUp()。如果我将 btnSubmit 放在表单标签内它不会在 javascript 上调用 Save(),而是在表单提交时调用 PopUp() 方法。我需要通过 Json 调用 PopUp() 并需要获取 Request.Files.Count .Pls 帮助。
@using (Html.BeginForm("PopUp", "EnergyCatagory", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div id="FixedHeightContainer">
<div id="FileUploadContainer">
</div>
<input type="button" id="btnAddAttachment" name="btnAddAttachment" class="ZWidgetTitle"
value="Add More Attachments" onclick="AddFileUpload()" />
</div>
<div id="NewAttachment">
<div style="background-color: #DADADA;">
<center>
<label style="font-family: Verdana; font-size: 13px; font-weight: bold; color: black;
height: 30px; width: 100%; padding-top: 20px;" id="lblMessage">
</label>
</center>
</div>
</div>
<div class="horizSep">
</div>
<div id="buttons">
</div>
} <button id="btnSubmit" class="buttons" onclick="Save()">
Attach</button>
function Save()
{$.ajax({
url: '/EnergyCatagory/PopUp',
type: 'POST',
contentType: 'application/json;',
dataType: 'json',
success: function (result) {
alert("success");
}
});
}控制器 - - -
public ActionResult PopUp()
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFileBase PostedFile = Request.Files[i];
if (PostedFile.ContentLength > 0)
{
string FileName = System.IO.Path.GetFileName(PostedFile.FileName);
var path1 = Path.Combine(Server.MapPath("~/Files/"), FileName);
//PostedFile.SaveAs(Server.MapPath("Files\\") + FileName);
PostedFile.SaveAs(path1);
return Json(
new
{
CustomMessage = "My message",
});
}
}