如何将图像文件和表单数据从 Ajax 传递到 MVC 控制器
How to Pass Image File and Form Data From Ajax to MVC Controller
我有一个带有图像和另一个字段的表单,我想在控制器中发送带有图像的数据。
<form id="first-form" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-9">
<div class="row">
<div class="col-sm-6 form-group">
@Html.Label(@RegisterResource.Name, new { @class = "control-label" })
<div class="field-input">
@Html.TextBox("Name", null, new { placeholder = @RegisterResource.Name, autofocus = "autofocus", @class = "input-text" })
@Html.ValidationMessage("Name", null, new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6 form-group">
@Html.Label(@RegisterResource.Family, new { @class = "control-label" })
<div class="field-input">
@Html.TextBox("Family", null, new { placeholder = @RegisterResource.Family, @class = "input-text" })
@Html.ValidationMessage("Family", null, new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
@Html.Label(@RegisterResource.CellPhone, new { @class = "control-label" })
<div class="field-input">
@Html.TextBox("CellPhone", null, new { placeholder = @RegisterResource.CellPhone, maxlength = 11, @class = "input-text", onkeydown = "return ValidateNumber(event);" })
@Html.ValidationMessage("CellPhone", null, new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6 form-group">
@Html.Label(@ContractorResource.City, new { @class = "control-label" })
<div class="field-input">
@Html.DropDownList("CityId", (SelectList)ViewBag.City, new { placeholder = @ContractorResource.City, @class = "input-text" })
@Html.ValidationMessage("CityId", null, new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="col-md-3 text-center">
<img id="image_upload_preview" class="img-circle" src="~/Content/Images/contractor-avator.png" />
<br />
<input type="file" id="inputFile" style="max-width: 200px" />
<label for="inputFile" class="btn-2">انتخاب فایل تصویر</label>
</div>
</div>
<div class="order-complate text-center">
<button type="submit" class="awe-btn awe-btn-1 awe-btn-medium" id="user-register-btn" data-loading-text="<i class='fa fa-spinner fa-spin '></i> @PageResource.ConfirmCode">@PageResource.Continues</button>
</div>
</form>
并在 jquery
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#image_upload_preview').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
fileImage = input.files[0];
}
}
$("#inputFile").change(function() {
readURL(this);
});
submitHandler: function() {
$("#user-register-btn").button('loading');
var data = $('#first-form').serialize();
var formData = new FormData();
formdata.append("img", fileImage );
$.post("/Contractor/SendConfirmCode",
data,
function(result) {
}
else {
}
},
"json");
但在控制器中 Request.Files
是 Empty
。
您需要更改上传图片的脚本。
在此示例中,当您 select 使用文件控件 "inputFile" 在视图中提及文件时,会发生 jquery 文件更改事件。
View code for single file
<div class="col-md-3 text-center">
<img id="image_upload_preview" class="img-circle" src="~/Content/Images/contractor-avator.png" />
<br />
<input type="file" id="inputFile" style="max-width: 200px" />
<label for="inputFile" class="btn-2">انتخاب فایل تصویر</label>
</div>
View code for selection multiple file
<div class="col-md-3 text-center">
<img id="image_upload_preview" class="img-circle" src="~/Content/Images/contractor-avator.png" />
<br />
<input multiple="" type="file" id="inputFile" style="max-width: 200px" />
<label for="inputFile" class="btn-2">انتخاب فایل تصویر</label>
</div>
Jquery file change code in this example if you want to select multiple file or single file
/*FILE UPLOAD HERE*/
var alldata = []
$('#inputFile').change(function () {
try {
var formData = new FormData();
var totalFiles = document.getElementById('inputFile').files.length;
for (var i = 0; i < totalFiles; i++) {
var file = document.getElementById('inputFile').files[i];
formData.append("oHttpPostedFileBase", file);
}
$.ajax({
type: "POST",
url: '/Contractor/SendConfirmCode',
data: formData,
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
if (response != null) {
console.log(response);
$('#image_upload_preview').attr('src', response[0].FilePath);
}
else {
alert('No Response...!');
}
},
error: function (error) {
alert("error");
}
});
} catch (e) {
alert("File Upload Error" + e.message);
}
});
Add this code in controller file with this class
#region FileUpload handling
/*File Upload*/
[HttpPost]
public ActionResult BaseAutoFileUpload(HttpPostedFileBase[] oHttpPostedFileBase)
{
try
{
List<FileUploadClass> lstUploadFile = new List<FileUploadClass>();
string yourfilepathfolder = "~/Uploads/Junk/";
for (int i = 0; i < Request.Files.Count; i++)
{
FileUploadClass oFileUploadClass = new FileUploadClass();
var file = Request.Files[i];
var fileName = Path.GetFileName(file.FileName);
fileName = Guid.NewGuid().ToString() + "_" + fileName;
var path = Path.Combine(Server.MapPath(yourfilepathfolder), fileName);
file.SaveAs(path);
oFileUploadClass.FileName = fileName.Substring(37);
oFileUploadClass.FileNameForDelete = fileName;
oFileUploadClass.FilePath = path;
lstUploadFile.Add(oFileUploadClass);
}
return Json(lstUploadFile);
}
catch (Exception ex)
{
return Json(null);
}
}
public class FileUploadClass
{
public string FileName { get; set; }
public string FilePath { get; set; }
public string FileNameForDelete { get; set; }
}
#endregion
注意:当您 select 归档时,您的文件会自动保存在 "yourfilepathfolder " 中给定特定路径的服务器上。
如果您有单个文件 selection 并且您不会显示您有哪个文件 selected
只需在代码中添加这一行
$('#image_upload_preview').attr('src', response[0].FilePath);
我已经为您添加了这一行,请检查。
签到控制台
感谢@jishan的全面解答;
如果我是你,我更喜欢用于发布表单数据的视图模型,但是,
根据您的看法:
$('#user-register-btn').click(function (e) {
e.preventDefault();
$("#user-register-btn").button('loading');
//var data = $('#first-form').serialize();
var data = {
Name: $('#Name').val(),
Family: $('#Family').val(),
CityId: $('#CityId').val(),
CellPhone: $('#CellPhone').val()
};
var formData = new FormData();
formData.append("data", JSON.stringify(data));
var totalFiles = document.getElementById('inputFile').files.length;
for (var i = 0; i < totalFiles; i++) {
var file = document.getElementById('inputFile').files[i];
formData.append("httpPostedFileBase", file);
}
$.ajax({
type: "POST",
url: '/Contractor/SendConfirmCode',
data: formData,
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
if (response != null) {
console.log(response);
$('#image_upload_preview').attr('src', response[0].FilePath);
}
else {
alert('No Response...!');
}
},
error: function (error) {
alert("error");
}
});
});
和在您的控制器中:
[HttpPost]
public ActionResult SendConfirmCode(HttpPostedFileBase[] httpPostedFileBase, string data)
{
if(Request.Files.Count > 0)
{
// as you wish
}
ModelForm formData = JsonConvert.DeserializeObject<ModelForm>(data);
// save formData to DB or ...
return View();
}
并且,模型 class 匹配表单数据:
public class ModelForm
{
public string Name { get; set; }
public string Family { get; set; }
public string CityId { get; set; }
public string CellPhone { get; set; }
}
我有一个带有图像和另一个字段的表单,我想在控制器中发送带有图像的数据。
<form id="first-form" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-9">
<div class="row">
<div class="col-sm-6 form-group">
@Html.Label(@RegisterResource.Name, new { @class = "control-label" })
<div class="field-input">
@Html.TextBox("Name", null, new { placeholder = @RegisterResource.Name, autofocus = "autofocus", @class = "input-text" })
@Html.ValidationMessage("Name", null, new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6 form-group">
@Html.Label(@RegisterResource.Family, new { @class = "control-label" })
<div class="field-input">
@Html.TextBox("Family", null, new { placeholder = @RegisterResource.Family, @class = "input-text" })
@Html.ValidationMessage("Family", null, new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
@Html.Label(@RegisterResource.CellPhone, new { @class = "control-label" })
<div class="field-input">
@Html.TextBox("CellPhone", null, new { placeholder = @RegisterResource.CellPhone, maxlength = 11, @class = "input-text", onkeydown = "return ValidateNumber(event);" })
@Html.ValidationMessage("CellPhone", null, new { @class = "text-danger" })
</div>
</div>
<div class="col-sm-6 form-group">
@Html.Label(@ContractorResource.City, new { @class = "control-label" })
<div class="field-input">
@Html.DropDownList("CityId", (SelectList)ViewBag.City, new { placeholder = @ContractorResource.City, @class = "input-text" })
@Html.ValidationMessage("CityId", null, new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="col-md-3 text-center">
<img id="image_upload_preview" class="img-circle" src="~/Content/Images/contractor-avator.png" />
<br />
<input type="file" id="inputFile" style="max-width: 200px" />
<label for="inputFile" class="btn-2">انتخاب فایل تصویر</label>
</div>
</div>
<div class="order-complate text-center">
<button type="submit" class="awe-btn awe-btn-1 awe-btn-medium" id="user-register-btn" data-loading-text="<i class='fa fa-spinner fa-spin '></i> @PageResource.ConfirmCode">@PageResource.Continues</button>
</div>
</form>
并在 jquery
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#image_upload_preview').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
fileImage = input.files[0];
}
}
$("#inputFile").change(function() {
readURL(this);
});
submitHandler: function() {
$("#user-register-btn").button('loading');
var data = $('#first-form').serialize();
var formData = new FormData();
formdata.append("img", fileImage );
$.post("/Contractor/SendConfirmCode",
data,
function(result) {
}
else {
}
},
"json");
但在控制器中 Request.Files
是 Empty
。
您需要更改上传图片的脚本。
在此示例中,当您 select 使用文件控件 "inputFile" 在视图中提及文件时,会发生 jquery 文件更改事件。
View code for single file
<div class="col-md-3 text-center">
<img id="image_upload_preview" class="img-circle" src="~/Content/Images/contractor-avator.png" />
<br />
<input type="file" id="inputFile" style="max-width: 200px" />
<label for="inputFile" class="btn-2">انتخاب فایل تصویر</label>
</div>
View code for selection multiple file
<div class="col-md-3 text-center">
<img id="image_upload_preview" class="img-circle" src="~/Content/Images/contractor-avator.png" />
<br />
<input multiple="" type="file" id="inputFile" style="max-width: 200px" />
<label for="inputFile" class="btn-2">انتخاب فایل تصویر</label>
</div>
Jquery file change code in this example if you want to select multiple file or single file
/*FILE UPLOAD HERE*/
var alldata = []
$('#inputFile').change(function () {
try {
var formData = new FormData();
var totalFiles = document.getElementById('inputFile').files.length;
for (var i = 0; i < totalFiles; i++) {
var file = document.getElementById('inputFile').files[i];
formData.append("oHttpPostedFileBase", file);
}
$.ajax({
type: "POST",
url: '/Contractor/SendConfirmCode',
data: formData,
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
if (response != null) {
console.log(response);
$('#image_upload_preview').attr('src', response[0].FilePath);
}
else {
alert('No Response...!');
}
},
error: function (error) {
alert("error");
}
});
} catch (e) {
alert("File Upload Error" + e.message);
}
});
Add this code in controller file with this class
#region FileUpload handling
/*File Upload*/
[HttpPost]
public ActionResult BaseAutoFileUpload(HttpPostedFileBase[] oHttpPostedFileBase)
{
try
{
List<FileUploadClass> lstUploadFile = new List<FileUploadClass>();
string yourfilepathfolder = "~/Uploads/Junk/";
for (int i = 0; i < Request.Files.Count; i++)
{
FileUploadClass oFileUploadClass = new FileUploadClass();
var file = Request.Files[i];
var fileName = Path.GetFileName(file.FileName);
fileName = Guid.NewGuid().ToString() + "_" + fileName;
var path = Path.Combine(Server.MapPath(yourfilepathfolder), fileName);
file.SaveAs(path);
oFileUploadClass.FileName = fileName.Substring(37);
oFileUploadClass.FileNameForDelete = fileName;
oFileUploadClass.FilePath = path;
lstUploadFile.Add(oFileUploadClass);
}
return Json(lstUploadFile);
}
catch (Exception ex)
{
return Json(null);
}
}
public class FileUploadClass
{
public string FileName { get; set; }
public string FilePath { get; set; }
public string FileNameForDelete { get; set; }
}
#endregion
注意:当您 select 归档时,您的文件会自动保存在 "yourfilepathfolder " 中给定特定路径的服务器上。
如果您有单个文件 selection 并且您不会显示您有哪个文件 selected
只需在代码中添加这一行
$('#image_upload_preview').attr('src', response[0].FilePath);
我已经为您添加了这一行,请检查。
签到控制台
感谢@jishan的全面解答;
如果我是你,我更喜欢用于发布表单数据的视图模型,但是,
根据您的看法:
$('#user-register-btn').click(function (e) {
e.preventDefault();
$("#user-register-btn").button('loading');
//var data = $('#first-form').serialize();
var data = {
Name: $('#Name').val(),
Family: $('#Family').val(),
CityId: $('#CityId').val(),
CellPhone: $('#CellPhone').val()
};
var formData = new FormData();
formData.append("data", JSON.stringify(data));
var totalFiles = document.getElementById('inputFile').files.length;
for (var i = 0; i < totalFiles; i++) {
var file = document.getElementById('inputFile').files[i];
formData.append("httpPostedFileBase", file);
}
$.ajax({
type: "POST",
url: '/Contractor/SendConfirmCode',
data: formData,
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
if (response != null) {
console.log(response);
$('#image_upload_preview').attr('src', response[0].FilePath);
}
else {
alert('No Response...!');
}
},
error: function (error) {
alert("error");
}
});
});
和在您的控制器中:
[HttpPost]
public ActionResult SendConfirmCode(HttpPostedFileBase[] httpPostedFileBase, string data)
{
if(Request.Files.Count > 0)
{
// as you wish
}
ModelForm formData = JsonConvert.DeserializeObject<ModelForm>(data);
// save formData to DB or ...
return View();
}
并且,模型 class 匹配表单数据:
public class ModelForm
{
public string Name { get; set; }
public string Family { get; set; }
public string CityId { get; set; }
public string CellPhone { get; set; }
}