Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# httppostedfilebase在模型中为空_C#_Ajax_Asp.net Mvc 5_Model_Multifile Uploader - Fatal编程技术网

C# httppostedfilebase在模型中为空

C# httppostedfilebase在模型中为空,c#,ajax,asp.net-mvc-5,model,multifile-uploader,C#,Ajax,Asp.net Mvc 5,Model,Multifile Uploader,我在我的项目中使用ajax post,即使图像和文本部分在模型中也是有效的,额外的附件为空,在错误部分,错误数量等于我发送到后端的附件数量,并且似乎是对象文件ModelState.values[8]Value.AttemptedValue=[object File],[object File],[object File],[对象文件]、[对象文件]……等等 我的控制器 [HttpPost] public ActionResult CreateTheIdea(OneIdeaModel mo

我在我的项目中使用ajax post,即使图像和文本部分在模型中也是有效的,额外的附件为空,在错误部分,错误数量等于我发送到后端的附件数量,并且似乎是对象文件ModelState.values[8]Value.AttemptedValue=[object File],[object File],[object File],[对象文件]、[对象文件]……等等

我的控制器

[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");
        }
    });
}

假设您正在尝试上载多个文件并尝试绑定到“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.");
    }
}
函数sendbasideainfo(){ if(window.FormData!==未定义){ var totalFiles=document.getElementById(“coverImage”).files.length; var covImg=document.getElementById(“coverImage”).files[0]; //创建FormData对象 var newForm=new FormData(); newForm.append(“title”,$(“#title”).val(); newForm.append(“coverImage”,covImg); append(“description”,encodeURI($(“#description”).val()); append(“whyThis”,encodeURI($(“#whyThis”).val()); append(“detail”,encodeURI($(“#detail”).val()); append(“ideaType”,encodeURI(ideaType)); append(“contentId”,encodeURI(contentId)); append(“status”,encodeURI(recordType)); var totalFiles=document.getElementById(“uploadBtn”).files.length; console.log(totalFiles); 如果(totalFiles>0){ 对于(var i=0;i试试这个

if (totalFiles > 0) {
        newForm.append("numberOfAttachment", totalFiles);
        for (var i = 0; i < totalFiles; i++) {
            newForm.append("attachment[" + i + "]", document.getElementById("uploadBtn").files[i]);
        }
    }
if(totalFiles>0){
append(“numberOfAttachment”,totalFiles);
对于(var i=0;i
您需要将每个单独的文件追加到
FormData
(您不能追加数组)但是我不知道@StephenMuecke会上传多少个文件,你还有什么建议吗?你只需将每个文件添加到
for
loop@StephenMuecke如果您对(var i=0;i(但您的问题中有太多没有意义的其他代码,如果您生成了正确的视图,那么将只是
var formdata=new formdata($('form').get(0));
来序列化所有内容(请参阅)
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.");
    }
}
if (totalFiles > 0) {
        newForm.append("numberOfAttachment", totalFiles);
        for (var i = 0; i < totalFiles; i++) {
            newForm.append("attachment[" + i + "]", document.getElementById("uploadBtn").files[i]);
        }
    }