Asp.net mvc 在jquery插件中使用fileinput的asp.net mvc Ajax提交表单

Asp.net mvc 在jquery插件中使用fileinput的asp.net mvc Ajax提交表单,asp.net-mvc,Asp.net Mvc,我的表格有问题 我需要提交一个带有ajax的表单,表单中有fileinput。在这个表单中,我使用带有fileinput的插件 这是我的密码: <link href="~/Content/Plugin/bootstrap-fileinput-master/css/fileinput.min.css" rel="stylesheet" /> <script src="~/Content/Plugin/bootstrap-fileinput-master/js/fileinput.

我的表格有问题

我需要提交一个带有
ajax
的表单,表单中有fileinput。在这个表单中,我使用带有fileinput的插件

这是我的密码:

<link href="~/Content/Plugin/bootstrap-fileinput-master/css/fileinput.min.css" rel="stylesheet" />
<script src="~/Content/Plugin/bootstrap-fileinput-master/js/fileinput.min.js"></script>
<script src="~/Content/Plugin/bootstrap-fileinput-master/js/fileinput_locale_zh.js"></script>

    @using (Ajax.BeginForm(null, null, new AjaxOptions(){
            HttpMethod = "post",Url = Url.Action("Upload", "WorkItem"),
                InsertionMode = InsertionMode.Replace, LoadingElementDuration = 2000,
                OnSuccess = "completed" },
                new { role = "form", enctype = "multipart/form-data" }))
        {
        <div class="input-group">
            <span class="input-group-addon" id="basic-addon1">TITLE</span>
            <input type="text" name="Descr" class="form-control" aria-describedby="basic-addon1">
        </div>
        <div class="m-b-5"></div>

        <div class="input-group">
            <span class="input-group-addon" id="basic-addon1">POINT</span>
            <input type="text" name="Point" class="form-control" aria-describedby="basic-addon1">
        </div>
        <div class="m-b-5"></div>
        <div class="input-group">
            <span class="input-group-addon" id="basic-addon1">DESCR</span>
            <input type="text" name="Descr" class="form-control" aria-describedby="basic-addon1">
        </div>
        <div class="m-b-5"></div>
        <input id="file-0a" name="file" class="file" type="file" data-min-file-count="1">
        <br />
        <button type="submit" class="btn btn-primary">Submit</button>
        <button type="reset" class="btn btn-default">Reset</button>
    }

@使用(Ajax.BeginForm)(null,null,new AjaxOptions(){
HttpMethod=“post”,Url=Url.Action(“上传”、“工作项”),
InsertionMode=InsertionMode.Replace,LoadingElementDuration=2000,
OnSuccess=“completed”},
新的{role=“form”,enctype=“multipart/form data”})
{
标题
指向
描述

提交 重置 }

单击“提交”按钮时,无法接受任何文件。出了什么问题?

正如@Stepher Muecke告诉
@Ajax的那样。BeginForm
不能用于发布文件。 我对插件没有概念。我使用以下方法:

    $("#btnUploadExcel").click(function () {

            if ($("#newuploadexcel").val() == '') {
                notie.alert(2, "Please Select Any File", 2);
            }
            else {

                if (window.FormData!= undefined) {

                    var fileUpload = $("#newuploadexcel").get(0);
                    var files = fileUpload.files;

                    // Create FormData object
                    var fileData = new FormData();

                    // Looping over all files and add it to FormData object
                    for (var i = 0; i < files.length; i++) {
                        fileData.append(files[i].name, files[i]);
                    }

                    // Adding one more key to FormData object
                    //      fileData.append('contentId', contentId); commented as now supplierId is passed in the excel itself

                    $.ajax({
                        url: '/BulkStock/UploadExcel',
                        data: fileData,
                        type: "POST",
                        async: true,
                        dataType: 'json',
                        contentType: false,
                        processData: false,
                        success: function (result) {

                            var data = result.message;
                            //1=Failure, No excel
                            //2= Failue, with excel
                            //3=success, no excel

                            if (result.errmsg == '3') {
                                notie.alert(1, data, 6);
                            }
                            else if (result.errmsg == '1') {
                                notie.alert(3, data, 6);
                            }
                            else {
                                window.location = result.link;
                                notie.alert(3, data, 10);
                            }

                        },
                        error: function (response) {

                            console.log(response.responseText);
                        },
                        failure: function (response) {
                            console.log(response.responseText);
                        }
                    });

                    $("#newUpload").modal('hide');

                } else {
                    notie.alert(3, "FormData is not supported.", 3);
                }
            }
        });
$(“#btnUploadExcel”)。单击(函数(){
if($(“#newuploadexcel”).val()=''){
通知警报(2,“请选择任何文件”,2);
}
否则{
if(window.FormData!=未定义){
var fileUpload=$(“#newuploadexcel”).get(0);
var files=fileUpload.files;
//创建FormData对象
var fileData=new FormData();
//循环所有文件并将其添加到FormData对象
对于(var i=0;i
我要获取文件的控制器是:

    public JsonResult UploadExcel()
        {
            string filePath = String.Empty;
            string fileName = string.Empty;
            if (Request.Files.Count > 0)
            {
                    //  Get all files from Request object  
                    HttpFileCollectionBase files = Request.Files;

                    for (int i = 0; i < files.Count; i++)
                    {
                        HttpPostedFileBase file = files[i];
                        fileName = file.FileName;
                        string extension = System.IO.Path.GetExtension(fileName);
                        if (extension.Equals(".xls") || extension.Equals(".xlsx"))
                        {
                            var now = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
                            string my3DigitRandomNumber = now.Substring(now.Length - 7, 3);
                            fileName = (file.FileName.Replace(extension, "")) + (my3DigitRandomNumber + extension);
                            filePath = string.Format("{0}/{1}", Server.MapPath("~/excelfiles"), fileName);
                            file.SaveAs(filePath);
                          }
                    }
              }
    }
Create a folder with the name "excelfiles" in your solution
public JsonResult UploadExcel()
{
string filePath=string.Empty;
字符串文件名=string.Empty;
如果(Request.Files.Count>0)
{
//从请求对象获取所有文件
HttpFileCollectionBase files=Request.files;
对于(int i=0;i
正如@Stepher Muecke告诉
@Ajax.BeginForm
不能用于发布文件。 我对插件没有概念。我使用以下方法:

    $("#btnUploadExcel").click(function () {

            if ($("#newuploadexcel").val() == '') {
                notie.alert(2, "Please Select Any File", 2);
            }
            else {

                if (window.FormData!= undefined) {

                    var fileUpload = $("#newuploadexcel").get(0);
                    var files = fileUpload.files;

                    // Create FormData object
                    var fileData = new FormData();

                    // Looping over all files and add it to FormData object
                    for (var i = 0; i < files.length; i++) {
                        fileData.append(files[i].name, files[i]);
                    }

                    // Adding one more key to FormData object
                    //      fileData.append('contentId', contentId); commented as now supplierId is passed in the excel itself

                    $.ajax({
                        url: '/BulkStock/UploadExcel',
                        data: fileData,
                        type: "POST",
                        async: true,
                        dataType: 'json',
                        contentType: false,
                        processData: false,
                        success: function (result) {

                            var data = result.message;
                            //1=Failure, No excel
                            //2= Failue, with excel
                            //3=success, no excel

                            if (result.errmsg == '3') {
                                notie.alert(1, data, 6);
                            }
                            else if (result.errmsg == '1') {
                                notie.alert(3, data, 6);
                            }
                            else {
                                window.location = result.link;
                                notie.alert(3, data, 10);
                            }

                        },
                        error: function (response) {

                            console.log(response.responseText);
                        },
                        failure: function (response) {
                            console.log(response.responseText);
                        }
                    });

                    $("#newUpload").modal('hide');

                } else {
                    notie.alert(3, "FormData is not supported.", 3);
                }
            }
        });
$(“#btnUploadExcel”)。单击(函数(){
if($(“#newuploadexcel”).val()=''){
通知警报(2,“请选择任何文件”,2);
}
否则{
if(window.FormData!=未定义){
var fileUpload=$(“#newuploadexcel”).get(0);
var files=fileUpload.files;
//创建FormData对象
var fileData=new FormData();
//循环所有文件并将其添加到FormData对象
对于(var i=0;i