gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:50:34 24 4
gpt4 key购买 nike

我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com