Jquery 使用Ajax J查询将文件路径从视图传递到MVC控制器返回null
我试图使用MVC5和Ajax Jquery(异步)上传一个图像,但图像变量的值总是返回null 关于这个问题,我已经检查了一些以前的堆栈溢出帖子,但是我找不到我的错误,有人能帮我吗 请帮忙 模型类Jquery 使用Ajax J查询将文件路径从视图传递到MVC控制器返回null,jquery,asp.net-ajax,jquery-file-upload,jsajaxfileuploader,Jquery,Asp.net Ajax,Jquery File Upload,Jsajaxfileuploader,我试图使用MVC5和Ajax Jquery(异步)上传一个图像,但图像变量的值总是返回null 关于这个问题,我已经检查了一些以前的堆栈溢出帖子,但是我找不到我的错误,有人能帮我吗 请帮忙 模型类 public class TravelCategoryCustom { public int categoryId { get; set; } public string categoryName { get; set; }
public class TravelCategoryCustom
{
public int categoryId { get; set; }
public string categoryName { get; set; }
public string categoryDescriprion { get; set; }
public HttpPostedFileBase image { get; set; }
public int TotalPlaces { get; set; }
}
查看
<form enctype="multipart/form-data">
<div class="form-group">
<label>Category Name</label>
@Html.TextBoxFor(model => model.categoryName, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Category Description</label>
@Html.TextAreaFor(model => model.categoryDescriprion, 5, 1, new { @class = "form-control " })
</div>
<div class="form-group">
<label>Category Image</label>
<input type="file" id="dialog" />
</div>
<input type="button" value="Create" id="submtt" class="btn btn-primary" onclick="favfunct()" />
</form>
类别名称
@Html.TextBoxFor(model=>model.categoryName,新的{@class=“form control”})
类别说明
@TextAreaFor(model=>model.categorydescription,5,1,新的{@class=“formcontrol”})
类别图像
脚本
<script>
function favfunct() {
$.ajax({
var formData = new FormData($('form')[0]);
url: "/MVCTravelCategories/Create",
dataType: "json",
type: "POST",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ trvlcategory: { categoryId: '1', categoryName: 'testName', categoryDescriprion: 'TestDec', image: formData , TotalPlaces: '3' } }),
async: true,
processData: false,
cache: false,
success: function (data) {
alert(data);
},
error: function (xhr) {
alert('error');
}
});
}
</script>
函数favfunct(){
$.ajax({
var formData=新的formData($('form')[0]);
url:“/MVCTravelCategories/Create”,
数据类型:“json”,
类型:“POST”,
contentType:'application/json;charset=utf-8',
数据:JSON.stringify({trvlcategory:{categoryId:'1',categoryName:'testName',categorydescription:'TestDec',image:formData,TotalPlaces:'3'}}),
async:true,
processData:false,
cache:false,
成功:功能(数据){
警报(数据);
},
错误:函数(xhr){
警报(“错误”);
}
});
}
行动结果
(图片)
谢谢大家检查我的问题,最后我找到了答案,我把脚本改成了这个,效果很好
<script>
var myVariable ;
var Asd = 'Helslo';
$("#myImage").change(function () {
var file = this.files[0];
fileName = file.name;
myVariable = file;
size = file.size;
type = file.type;
myVariable = file;
});
function favfunct() {
var formData = new FormData();
var totalFiles = document.getElementById("dialog").files.length;
for (var i = 0; i < totalFiles; i++) {
var file = document.getElementById("dialog").files[i];
formData.append("dialog", file);
}
$.ajax({
url: "/MVCTravelCategories/Create",
dataType: "json",
type: "POST",
enctype: "multipart/form-data",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ trvlcategory: { categoryId: '1', categoryName: 'TestName', categoryDescriprion: 'TestDes', image: myVariable, TotalPlaces: '3' } }),
async: true,
processData: false,
cache: false,
success: function (data) {
alert(data);
},
error: function (xhr) {
alert(Asd.toString());
alert('error');
}
});
}
</script>
var-myVariable;
var Asd='Helslo';
$(“#myImage”).change(函数(){
var file=this.files[0];
fileName=file.name;
myVariable=文件;
size=file.size;
type=file.type;
myVariable=文件;
});
函数favfunct(){
var formData=new formData();
var totalFiles=document.getElementById(“dialog”).files.length;
对于(var i=0;i
您可以在代码隐藏中共享您的操作结果代码吗?但是,当使用为文件输入名attributeok user3468621提供的相同名称值时,您可以在代码中获取文件,我添加了我的操作结果的屏幕截图。谢谢Suser3468621,我怎样才能从代码隐藏中获得它,你能给我一个例子吗