jquery - 使用 $.Post 或 $.Ajax 传递 HttpPostedFileBase 类型

标签 jquery asp.net-mvc razor

我的 Controller 中有此操作。

    public ActionResult Upload(HttpPostedFileBase fileData)
    {
        //code for uploading goes here.
        return View();
    }

有没有办法使用 $.post 或 $.Ajax 访问此操作? 在我的 View 中,我有这些代码。

<input type="file" name="fileData" id="fileData" multiple/>
<button>Upload</button> 

首先我尝试使用此代码

$("button").on("click",function(){
   $.post('@Url.Action' + $(".filedata").val(),function(data){
      console.log(data);
   });
});

但是当我尝试在“上传”操作上断点时,“filedata”变量具有null值(value)。

不知道出了什么问题。希望大家能够帮助我。

最佳答案

您需要有一个 IFRAME,用于使用该文件发布帖子。

像这样:

<iframe id="targetUpload" name="targetUpload"></iframe>

在表单中,您将目标设置为 iframe。

<form target="targetUpload" name="file" runat="server" method="post" id="file" enctype="multipart/form-data" action="@Url.Content("~/Controller/Action")">

还有一个提交按钮

<input type="Submit" value="Upload">

关于jquery - 使用 $.Post 或 $.Ajax 传递 HttpPostedFileBase 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11774489/

相关文章:

asp.net-mvc-3 - asp.net mvc 3 中元标记的正确方法

php - 使用ajax将数据发送到php时出现“不允许发布方法”错误

javascript - 如何设置 setTimeout 和clearTimeout 在同一按钮上运行

asp.net-mvc - 系统或配置设置的外部控制

jquery - MVC4 : jQuery Validation Unobtrusive Native working incorrectly

jquery - 无法访问 HttpRequest 中的 cookie

javascript - 为什么我的导航栏文本不在同一行?

javascript - 来自 span 的 Jquery 搜索字符串无法正常工作

javascript - 可以使用 .append() 但不能使用元素的 .style

c# - Controller 中带有 MVC 5 的 Unity DI