javascript - 在 asp .net mvc 中使用 jquery 将文件作为参数传递

标签 javascript jquery ajax asp.net-mvc

为了从excel文件导入数据并在ascx页面中显示数据,我想通过jQuery函数将文件作为参数传递给 Controller 这是我的代码: 查看:

<% Using (Html.BeginForm (new {enctype = "multipart / form-data"}))
                     {%>
                         <input type = "file" name = "file" />
                         <input type = "button" value = "OK" onclick = "Import (); return false;" />
                     <%}%>

jquery:

Import function () {
     var formData = new FormData ();
     formData = do something to get the file
     var Dialog = new MessageBox ();
     $ .ajax ({
         the type: 'POST'
         url: "/ Controller / Import"
         data: formData,
         cache: false,
         contentType: false,
         processData: false,
         success: function (View) {
             alert ("success");
             $ ("# DivList"). Empty ()
             $ ("# mainPageMainContent") append (View).;
         },
         complete: function () {
             ClosePatienter ();
             Dialog.MsgClose ();
         }
     });
}

Controller :

public ActionResult Import(HttpPostedFileBase file)
{
      ......
      return PartialView("OtherView", model);
}

当我执行此操作时, Controller 中的文件为空 有人可以帮助我 预先感谢您

最佳答案

您没有将文件添加到表单数据对象中。做这样的事情:

var formData = new FormData ();
//formData = do something to get the file
var uploadField = document.getElementById('uploadField');
formData.append("file", uploadField.files[0]);

其中“uploadField”是文件输入元素的 ID。

关于javascript - 在 asp .net mvc 中使用 jquery 将文件作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27019745/

相关文章:

JavaScript 错误/故障?

javascript - 处理重复任务的警报模式

javascript - 在 uiwebview ios 中采购本地 javascript

javascript - 挑选中奖彩票时出现数组错误。

php - 在 php mysql 的引导选项卡中使用 ajax 调用加载数据

php - 无法读取未定义的属性 'readyState'

javascript - 在新标签页中打开一个 html 并访问它的 DOM 元素

javascript - 在单个全局事件处理程序中捕获所有点击事件是不是一个坏主意?

jquery - $.cache 和 $.data 有什么区别?

php - yii2中Ajax保存到数据库