0

我在 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",

           });
          }
        }
4

1 回答 1

0

如果表单正在提交并且不调用函数 onclick 是因为输入类型是按钮,您可以将其更改为 html 链接标记。

也调用内联 javascript (onclick) 不是一个好习惯,我注意到你使用 jquery 那么为什么不使用 jquery 呢?

function Save(){
 $.ajax({
        url: '/EnergyCatagory/PopUp',
        type: 'POST',
        contentType: 'application/json;',
        dataType: 'json',
        success: function (result) {
        alert("success");
        }
  });

  $("#btnSubmit").click(function(){
   Save();
});



@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>

 <a id="btnSubmit" class="buttons">Attach</a>       
}   

我认为你得到Request.Files.Count等于零是因为你没有任何输入类型文件?你?

于 2013-03-21T03:33:38.543 回答