Asp.net mvc 4 在jquery模式对话框窗口中上载MVC4文件

Asp.net mvc 4 在jquery模式对话框窗口中上载MVC4文件,asp.net-mvc-4,razor,file-upload,Asp.net Mvc 4,Razor,File Upload,我正在开发一个基于MVC/Razor的应用程序 我试图在jquery模式对话框中的视图中设置文件上载 这是我的视图代码 @using (Html.BeginForm("<MyAction>", "<MyController>", FormMethod.Post, new { enctype = "multipart/form-data" })) { <div> <input type="file" id="UploadImage"

我正在开发一个基于MVC/Razor的应用程序 我试图在jquery模式对话框中的视图中设置文件上载

这是我的视图代码

@using (Html.BeginForm("<MyAction>", "<MyController>", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        <input type="file" id="UploadImage" name="UploadImage" style="width:705px;" />
    </div>
    <div>
        <input id="sbmt" type="submit" value="Save" />
    </div>
}
我错过了什么


感谢

我能够在JQuery模式对话框(MVC)中执行文件上载,方法是将表单数据显式添加到Ajax帖子中:

Javascript代码:

     // Checking whether FormData is available in browser  
                if (window.FormData !== undefined) {
                    var fileUpload = $("#fileInput").get(0);
                    var files = fileUpload.files;

                    // Create FormData object  
                    var fileData = new FormData();

                    // Looping over all files and add it to FormData object  
                    for (var i = 0; i < files.length; i++) {
                        fileData.append(files[i].name, files[i]);
                    }

                    // Adding additional parameters to FormData object  

                    fileData.append('name', $('#nameinput').val());  
                    fileData.append('uniqueID', $('#hiddenFieldUniqueID').val());  


                $.ajax({
                    type: 'POST',
                    contentType: false,
                    processData: false,
                    url: '@Url.Action("UploadFile", "YourController")',
                    data: fileData, 
                    success: function (returnValues) {
                        $('.ui-dialog-buttonpane').unblock();
                        if (returnValues["success"] ==  true) {
                            bootbox.alert(returnValues["feedback"]);
                            $(dlg).dialog("close");
                        }
                        else {
                            bootbox.alert(returnValues["feedback"]);
                        }
                    },
                    error: function (returnValue) {
                        $('.ui-dialog-buttonpane').unblock();
                        debugger;
                        bootbox.alert({ message: "Oops - Error Occured!" + returnValue, size: 'small' });
                    }
                    });
                }
                else {
                    bootbox.alert("Your browser doesnt support the method we are using to upload files (FormData is not supported)");
                }  

这种方法的功劳就在这里:

我能够在JQuery模式对话框(MVC)中执行文件上传,方法是将表单数据显式添加到Ajax帖子中:

Javascript代码:

     // Checking whether FormData is available in browser  
                if (window.FormData !== undefined) {
                    var fileUpload = $("#fileInput").get(0);
                    var files = fileUpload.files;

                    // Create FormData object  
                    var fileData = new FormData();

                    // Looping over all files and add it to FormData object  
                    for (var i = 0; i < files.length; i++) {
                        fileData.append(files[i].name, files[i]);
                    }

                    // Adding additional parameters to FormData object  

                    fileData.append('name', $('#nameinput').val());  
                    fileData.append('uniqueID', $('#hiddenFieldUniqueID').val());  


                $.ajax({
                    type: 'POST',
                    contentType: false,
                    processData: false,
                    url: '@Url.Action("UploadFile", "YourController")',
                    data: fileData, 
                    success: function (returnValues) {
                        $('.ui-dialog-buttonpane').unblock();
                        if (returnValues["success"] ==  true) {
                            bootbox.alert(returnValues["feedback"]);
                            $(dlg).dialog("close");
                        }
                        else {
                            bootbox.alert(returnValues["feedback"]);
                        }
                    },
                    error: function (returnValue) {
                        $('.ui-dialog-buttonpane').unblock();
                        debugger;
                        bootbox.alert({ message: "Oops - Error Occured!" + returnValue, size: 'small' });
                    }
                    });
                }
                else {
                    bootbox.alert("Your browser doesnt support the method we are using to upload files (FormData is not supported)");
                }  

这种方法的功劳就在这里:

这是视图中唯一的
元素吗?好吧,直接在这个视图代码上没有元素……当然你有一个
元素-这就是
Html.BeginForm()
生成的。好的,但除此之外,没有,我没有任何其他元素我想你的表单中除了文件输入之外还有其他元素?您的参数
模型
是否正确绑定?如果在方法中包含一个参数
HttpPostedFileBase uploadImage
,会发生什么?这是视图中唯一的
元素吗?好的,直接在这个视图代码上没有元素…当然你有一个
元素-这就是
Html.BeginForm()
生成的。好的,但除此之外,没有,我没有任何其他元素我想你的表单中除了文件输入之外还有其他元素?您的参数
模型
是否正确绑定?如果在方法中包含一个参数
HttpPostedFileBase uploadImage
,会发生什么?
<div class="col-md-9">
                <label class="btn btn-primary" for="fileInput">
                    <input id="fileInput" type="file" style="display:none"
                           onchange="$('#upload-file-info').html(this.files[0].name)">
                    Select
                </label>
                <span class='label label-info' id="upload-file-info"></span>
            </div>
      [HttpPost]
    public ActionResult UploadFile()
    {
        YourObjectFile yourObjectFile = null;
        try
        {
            string name = Request.Form["name"];
            if (Request.Files.Count > 0)
            {
                yourObjectFile = new YourObjectFile ();
                HttpPostedFileBase file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    string fileName = file.FileName;
                    using (var reader = new System.IO.BinaryReader(file.InputStream))
                    {
                        yourObjectFile.RawData = reader.ReadBytes(file.ContentLength);
                    }
                }
            }                
    .......