通过ajax将文件发送到另一个动作MVC#
我试图通过ajax将一个文件发送到控制器中的一个动作,我知道关于这个主题存在很多问题,但没有一个解决了我的问题 我有一个视图,用户将上传一个文件,我正试图在控制器中获取该文件,但由于Request.Files.Count(),我总是得到0 ajax中调用的操作与实际视图不同 我将MVC5与ASP.NET和C一起使用# 希望你能帮助我。谢谢 我的看法是:通过ajax将文件发送到另一个动作MVC#,c#,ajax,asp.net-mvc-5,C#,Ajax,Asp.net Mvc 5,我试图通过ajax将一个文件发送到控制器中的一个动作,我知道关于这个主题存在很多问题,但没有一个解决了我的问题 我有一个视图,用户将上传一个文件,我正试图在控制器中获取该文件,但由于Request.Files.Count(),我总是得到0 ajax中调用的操作与实际视图不同 我将MVC5与ASP.NET和C一起使用# 希望你能帮助我。谢谢 我的看法是: `@using (Html.BeginForm("editProject", "Projects", new { area = "admin"
`@using (Html.BeginForm("editProject", "Projects", new { area = "admin" },
FormMethod.Post, new { @class = "form-horizontal", role = "form", enctype =
"multipart/form-data" }))
{
@Html.AntiForgeryToken()
<input type="file" id="inpFile" name="attachment" multiple>
}`
`@使用(Html.BeginForm(“editProject”,“Projects”,new{area=“admin”},
FormMethod.Post,新的{@class=“form horizontal”,role=“form”,enctype=
“多部分/表单数据”})
{
@Html.AntiForgeryToken()
}`
Javascript:
var res=document.getElementById(“inpFile”);
res.addEventListener(“更改”,函数(){
files=document.getElementById(“inpFile”).files;
数据=新表单数据();
data.append(文件[0]。名称,文件);
$.ajax({
url:“@url.Action(“保存文件”,“项目”,新建{area=“admin”})”,
类型:“POST”,
数据类型:“json”,
数据:数据,
contentType:false,
processData:false,
成功:功能(数据){
console.log(data.UploadedFileCount+)已上载的文件
成功);;
}
});
});
我为您的问题生成代码,希望它对您非常有帮助
控制器
public ActionResult Index()
{
return View();
}
public ActionResult Upload()
{
if (Request.Files["ChequeFile"].ContentLength > 0)
{
String path = "~/Content/";
var fileName = Path.GetFileName(Request.Files["ChequeFile"].FileName);
Random rnd = new Random();
int rndnumber = rnd.Next(1, 9999999);
var filepath = Path.Combine(path, rndnumber + "" + fileName);
if (System.IO.File.Exists(filepath))
{ System.IO.File.Delete(filepath); }
Request.Files["ChequeFile"].SaveAs(Server.MapPath(filepath));
return Json("File Successfully Upload via ajax.");
}
else
{
return Json("File Must Be Required");
}
}
看法
在MVC中通过Ajax上传文件#
上载文件
Jquery与Ajax请求
@section scripts{
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).on('click', "#uploadss", function () {
debugger;
var fdata = new FormData();
$('input[name="ChequeFile"]').each(function (a, b) {
var fileInput = $('input[name="ChequeFile"]')[a];
if (fileInput.files.length > 0) {
var file = fileInput.files[0];
fdata.append("ChequeFile", file);
}
});
$.ajax({
//cache: false,
//async: true,
type: "POST",
url: "/Home/Upload",
data: fdata,
contentType: false,
processData: false,
success: function (data) {
//debugger;
$("#notification").html('');
$("#notification").html(data);
},
error: function (data) {
$("#notification").html('');
$("#notification").html(data);
}
});
})
</script>
}
@节脚本{
$(文档)。在('单击',“#上载”,函数(){
调试器;
var fdata=新表单数据();
$('input[name=“ChequeFile”]”)。每个(函数(a,b){
var fileInput=$('input[name=“ChequeFile”]')[a];
如果(fileInput.files.length>0){
var file=fileInput.files[0];
fdata.append(“支票文件”,文件);
}
});
$.ajax({
//cache:false,
//async:true,
类型:“POST”,
url:“/Home/Upload”,
数据:fdata,
contentType:false,
processData:false,
成功:功能(数据){
//调试器;
$(“#通知”).html(“”);
$(“#通知”).html(数据);
},
错误:函数(数据){
$(“#通知”).html(“”);
$(“#通知”).html(数据);
}
});
})
}
如果你需要的代码,那么请我的github的故事,并找到该项目
@section scripts{
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).on('click', "#uploadss", function () {
debugger;
var fdata = new FormData();
$('input[name="ChequeFile"]').each(function (a, b) {
var fileInput = $('input[name="ChequeFile"]')[a];
if (fileInput.files.length > 0) {
var file = fileInput.files[0];
fdata.append("ChequeFile", file);
}
});
$.ajax({
//cache: false,
//async: true,
type: "POST",
url: "/Home/Upload",
data: fdata,
contentType: false,
processData: false,
success: function (data) {
//debugger;
$("#notification").html('');
$("#notification").html(data);
},
error: function (data) {
$("#notification").html('');
$("#notification").html(data);
}
});
})
</script>
}