我有一个MVC应用程序,我在应用程序中上传图片文件。
这方面的代码:
var files = $(".uploadFile").data("files");
$.each(files, function (key, value) {
data.append('file', value);
})
$('.userForm *').filter(':input').each(function (index, value) {
data.append(value.id, $("#" + value.id).val());
});
$.ajax({
url: "/Customer/AddCustomer",
type: 'POST',
data: data,
cache: false,
processData: false,
contentType: false,
success: function (data) {
// logic
$.LoadingOverlay("hide");
},
error: function (event, jqxhr, settings, thrownError) {
// logic
$.LoadingOverlay("hide");
}
});服务器代码:
public string AddCustomer(HttpPostedFileBase file, Customer customer)
{
// add customer and return to edit view
return partialView("customerEdit");
} 我正在试图弄清楚如何在我发送的每个文件中添加一个属性?例如,对于我添加的每个文件,我都会创建一个列表,并将其附加到DOM中。在这个列表中,每个文件旁边都有一个复选框,它指示这个文件(图片)是否应该是配置文件图片。
如何在每个文件中添加一个boelan属性?
发布于 2015-10-19 15:30:21
多亏了“adeneo”,我的大脑才有了起色。
我不得不在这里重新考虑我的策略,因为我不想把额外的参数作为串连字符串发送。
我做了以下工作:
在我的第一篇文章中,我发送了文件和客户数据。我将客户保存在DB中,对于文件,我将其存储在tempdata中,以便能够在我的第二篇文章中访问它,在那里我将使用所需的额外参数将该文件保存到数据库。
剧本:
$.ajax({
url: "/Customer/AddCustomer",
type: 'POST',
data: data,
cache: false,
processData: false,
contentType: false,
success: function (data) {
// "Pictures" is an array that contains objects with all the file names and other properties.
$.ajax({
url: "/Customer/AddCustomerPictures",
type: 'POST',
data: JSON.stringify({ pictureFiles: Pictures}),
contentType: 'application/json; charset=utf-8',
success: function(data) {
//logic
}
});
//logic
},
error: function (event, jqxhr, settings, thrownError) {
//logic
}
});服务器代码,请注意整个解决方案不包括:
public ActionResult AddCustomer(CustomerM customer)
{
var Id = _iScissorFingersManager.AddCustomer(customer.FirstName, customer.LastName, customer.Email,customer.PhoenNumber);
if (Request.Files.Count != 0)
{
TempData["files"] = Request.Files;
TempData["Id"] = Id;
}
// add customer and return to edit view
return PartialView("CustomerEdit", customer);
}
public ActionResult AddCustomerPictures(List<PictureFiles> pictureFiles)
{
var files = (HttpFileCollectionBase)TempData["files"];
var id = (long) TempData["Id"];
if (files != null)
{
foreach (var p in pictureFiles)
{
for (int i = 0; i < files.Count; i++)
{
HttpPostedFileBase hpf = files[i];
p.Name == files[i].FileName && p.IsProfile ? _iScissorFingersManager.UploadCustomerPictures(hpf.InputStream, hpf.ContentType, id, true), _iScissorFingersManager.UploadCustomerPictures(hpf.InputStream, hpf.ContentType, id);
}
}
}
return PartialView("CustomerProfileImage");
}https://stackoverflow.com/questions/33200780
复制相似问题