使用MVC3中的模式弹出文件上传文件

时间:2012-07-14 17:16:19

标签: jquery asp.net-mvc-3

我正在尝试使用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>

&#34;

脚本加载文件。

&#34;

<% 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>
<% } %>

&#34;

1 个答案:

答案 0 :(得分:2)

不幸的是,Ajax.BeginForm无法上传文件。你必须使用IFrame或类似的方法“假装”。尝试退房

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

例如。

干杯 菲尔