0

我正在尝试使用 MVC3 中的模式弹出窗口来实现文件上传。上传文件时,控制器接收 null 而不是文件名。这个功能可以在 MVC3 中实现吗?
jQuery如下,

控制器代码:

"

[HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {

            var fileName = "";
            var path = "";
            string file1 = "";
            if (ModelState.IsValid)
            {
                if (file.ContentLength > 0)
                {
                    fileName = Path.GetFileName(file.FileName);
                    path = Path.Combine(Server.MapPath("~/Content/"), fileName);
                    file.SaveAs(path);
                }
            }
            return RedirectToAction("Create","Manager",new{file1=fileName});
        }

" "

    var linkObj;
    $(function () {
        $(".editLink").button();

        $('#updateDialog').dialog({
            autoOpen: false,
            width: 400,
            resizable: false,
            modal: true,
            buttons: {
                "Update": function () {
                    $("#update-message").html(''); //make sure there is nothing on the message before we continue                         
                    $("#updateCarForm").submit();
                },
                "Cancel": function () {
                    $(this).dialog("close");
                }
            }
        });

        $(".editLink").click(function () {
            //change the title of the dialgo
            linkObj = $(this);
            var dialogDiv = $('#updateDialog');
            var viewUrl = linkObj.attr('href');
            $.get(viewUrl, function (data) {
                dialogDiv.html(data);
                //validation
                var $form = $("#updateCarForm");
                // Unbind existing validation
                $form.unbind();
                $form.data("validator", null);
                // Check document for changes
                $.validator.unobtrusive.parse(document);
                // Re add validation with changes
                $form.validate($form.data("unobtrusiveValidation").options);
                //open dialog
                dialogDiv.dialog('open');
            });
            return false;
        });

    });


    function updateSuccess() {
        if ($("#update-message").html() == "True") {
            //we update the table's info
            var parent = linkObj.closest("tr");
            parent.find(".carName").html($("#Name").val());
            parent.find(".carDescription").html($("#Description").val());
            //now we can close the dialog
            $('#updateDialog').dialog('close');
            //twitter type notification
            $('#commonMessage').html("Update Complete");
            $('#commonMessage').delay(400).slideDown(400).delay(3000).slideUp(400);
        }
        else {
            $("#update-message").show();
        }
    }

</script>

"

脚本加载文件。

"

<% using (Ajax.BeginForm("Upload", "Manager", null,
        new AjaxOptions
        {
            UpdateTargetId = "update-message",
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "POST",
            OnSuccess = "updateSuccess"
        }, new { @id = "updateCarForm" }))

    { %> 
    <div class="editor-field" id="error invisible"></div>
    <fieldset>
      <div>
        <input type="file" name="file" id="file"/>

      </div>
         <div>   
           <input type="submit"  value="Save" />
         </div>
    </fieldset>
<% } %>

"

4

1 回答 1

2

不幸的是 Ajax.BeginForm 不能上传文件。您将不得不使用 IFrame 或类似工具来“伪造”它。尝试签出

http://www.webtoolkit.info/ajax-file-upload.html

例如。

干杯菲尔

于 2012-07-16T14:17:07.647 回答