我正在我的项目中使用ajax发布,即使图像和文本部分在模型中有效,额外的附件为null,在错误部分中,错误的数量等于我发送到后端的附件的数量,似乎作为对象文件ModelState.values [8] Value.AttemptedValue = [对象文件],[对象文件],[对象文件],[对象文件],[对象文件] ....等等
我的控制器
[HttpPost]
public ActionResult CreateTheIdea(OneIdeaModel model)
{
string imgResult = "";
string attachmentResult = "OK";
if (ModelState.IsValid)
{
}
}
我的观点
function sendBasicIdeaInfo() {
var totalFiles = document.getElementById("coverImage").files.length;
var covImg = document.getElementById("coverImage").files[0];
newForm.append("title", $("#title").val());
newForm.append("coverImage", covImg);
newForm.append("description", encodeURI($("#description").val()));
newForm.append("whyThis", encodeURI($("#whyThis").val()));
newForm.append("detail", encodeURI($("#detail").val()));
newForm.append("ideaType", encodeURI(ideaType));
newForm.append("contentId", encodeURI(contentId));
newForm.append("status", encodeURI(recordType));
var totalFiles = document.getElementById("uploadBtn").files.length;
console.log(totalFiles);
if (totalFiles > 0) {
for (var i = 0; i < totalFiles; i++) {
files[i] = document.getElementById("uploadBtn").files[i];
//console.log(files["name"]);
}
newForm.append("attachment", files);
newForm.append("contentType", "@ContentTypeEnum.IDEA.ToString()");
//newForm.append("contentId", newIdeaId);
}
$.ajax({
type: 'post',
url: '/@activeLanguage/Idea/CreateTheIdea',
data: newForm,
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
if (Number.isInteger(parseInt(response))) {
complete();
}
else
{
openVSnackBar("@Resources.errAnError", "error");
}
},
error: function (error) {
openVSnackBar("@Resources.errAnError", "error");
}
});
}
我的模特
public class OneIdeaModel
{
//Idea text part
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
public string whyThis { get; set; }
public string detail { get; set; }
public string ideaType { get; set; }
public string contentId { get; set; }
public string status { get; set; }
public int attachmentCount { get; set; }
public string langCode { get; set; }
public int userId { get; set; }
//Idea attachment part
public string contentType { get; set; }
public virtual HttpPostedFileBase[] attachment { get; set; }
public string fileType { get; set; }
public string fileName { get; set; }
//Idea image part
public virtual HttpPostedFileBase thumbImage { get; set; }
public virtual HttpPostedFileBase coverImage { get; set; }
public bool isImageFormatValid(HttpPostedFileBase img)
{
List<string> types = new List<string>() { "jpeg", "jpg", "png" };
if (types.Contains(img.FileName.Split('.')[img.FileName.Split('.').Length - 1].ToLower())) return true;
else return false;
}
public bool isAttachmentFormatValid(HttpPostedFileBase attachment)
{
List<string> types = new List<string>() { "application/pdf", "text/plain", "application/vnd.ms-excel.sheet.binary.macroenabled.12", "application/vnd.ms-excel.addin.macroenabled.12", "application/vnd.ms-excel", "application/msword" };
if (types.Contains(attachment.ContentType)) return true;
else return false;
}
}
正如我所说,除附件外,所有值都来自。为什么没有iddk?如果可以的话,我会很感激
有效载荷
------WebKitFormBoundaryJBAmObnu25KFXIzw
Content-Disposition: form-data; name="title"
123
------WebKitFormBoundaryJBAmObnu25KFXIzw
Content-Disposition: form-data; name="coverImage";
filename="35205568_10156471428459494_5451669252295622656_n.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryJBAmObnu25KFXIzw
Content-Disposition: form-data; name="description"
123
------WebKitFormBoundaryJBAmObnu25KFXIzw
Content-Disposition: form-data; name="whyThis"
123
------WebKitFormBoundaryJBAmObnu25KFXIzw
Content-Disposition: form-data; name="detail"
123123123123
------WebKitFormBoundaryJBAmObnu25KFXIzw
Content-Disposition: form-data; name="ideaType"
Category
------WebKitFormBoundaryJBAmObnu25KFXIzw
Content-Disposition: form-data; name="contentId"
0
------WebKitFormBoundaryJBAmObnu25KFXIzw
Content-Disposition: form-data; name="status"
PUBLISHED
------WebKitFormBoundaryJBAmObnu25KFXIzw
Content-Disposition: form-data; name="attachment"
[object File],[object File],[object File]
------WebKitFormBoundaryJBAmObnu25KFXIzw
Content-Disposition: form-data; name="contentType"
IDEA
------WebKitFormBoundaryJBAmObnu25KFXIzw--
答案 0 :(得分:1)
尝试
if (totalFiles > 0) {
newForm.append("numberOfAttachment", totalFiles);
for (var i = 0; i < totalFiles; i++) {
newForm.append("attachment[" + i + "]", document.getElementById("uploadBtn").files[i]);
}
}
答案 1 :(得分:0)
假设您正在尝试上传多个文件并试图绑定到“ attachment”属性。这将是您案例的javascript代码-
function sendBasicIdeaInfo() {
if (window.FormData !== undefined) {
var totalFiles = document.getElementById("coverImage").files.length;
var covImg = document.getElementById("coverImage").files[0];
// Create FormData object
var newForm = new FormData();
newForm.append("title", $("#title").val());
newForm.append("coverImage", covImg);
newForm.append("description", encodeURI($("#description").val()));
newForm.append("whyThis", encodeURI($("#whyThis").val()));
newForm.append("detail", encodeURI($("#detail").val()));
newForm.append("ideaType", encodeURI(ideaType));
newForm.append("contentId", encodeURI(contentId));
newForm.append("status", encodeURI(recordType));
var totalFiles = document.getElementById("uploadBtn").files.length;
console.log(totalFiles);
if (totalFiles > 0) {
for (var i = 0; i < totalFiles; i++) {
files[i] = document.getElementById("uploadBtn").files[i];
newForm.append("attachment", files[i]);
//console.log(files["name"]);
}
//newForm.append("attachment", files);
//newForm.append("contentType", "@ContentTypeEnum.IDEA.ToString()");
//newForm.append("contentId", newIdeaId);
}
$.ajax({
type: 'POST',
url: '/@activeLanguage/Idea/CreateTheIdea',
data: newForm,
contentType: false, // Not to set any content header
processData: false, // Not to process data
success: function(response) {
if (Number.isInteger(parseInt(response))) {
complete();
} else {
openVSnackBar("@Resources.errAnError", "error");
}
},
error: function(error) {
openVSnackBar("@Resources.errAnError", "error");
}
});
} else {
alert("FormData is not supported.");
}
}