如何从Javascript向IE发送IEnumerable <httppostedfilebase>到Asp.net控制器?

时间:2017-08-09 16:52:04

标签: javascript c# asp.net asp.net-mvc

我在Html.BeginForm中有一个多输入文件。我将文件处理成Javascript数组,我需要发送此数组或更好,更改输入值。

  

HTML

@using (Html.BeginForm("Xml32", "Cfdi", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" id="files" accept=".xml,.XML" name="files" multiple />
    <button type="submit">Enviar </button>
}
<div class="container">
    <table id="myTable" class="table table-hover">
        <thead>
            <tr>
                <th>Nombre</th>
                <th>Tipo</th>
                <th>Tamaño</th>
                <th>Modificación</th>
            </tr>
        </thead>
        <tbody></tbody>

    </table>
</div>
  

脚本

var selDiv = "";
    var storedFiles = [];

    $(document).ready(function() {
        $("#files").on("change", handleFileSelect);

        selDiv = $("#myTable > tbody:last-child");

        $("tbody").on("click", ".selFile", removeFile);
    });

    function handleFileSelect(e) {
        var files = e.target.files;
        var filesArr = Array.prototype.slice.call(files);
        filesArr.forEach(function(f) { 
            storedFiles.push(f);

            var reader = new FileReader(e);
            reader.onload = function () {
                var html = '<tr class="selFile" data-file="' + f.name +'"><th>' + escape(f.name) + '</th ><th>' + f.type + '</th > <th>' + f.size + '</th ><th>' + f.lastModifiedDate.toLocaleDateString() + '</th></tr >';
                selDiv.append(html);

            }
            reader.readAsDataURL(f); 
        });

    }

    function removeFile(e) {
        var file = $(this).data("file");
        for(var i=0;i<storedFiles.length;i++) {
            if(storedFiles[i].name === file) {
                storedFiles.splice(i,1);
                break;
            }
        }
        $(this).remove();
    }
  

Asp控制器操作

[HttpPost]
    public ActionResult Xml32(IEnumerable<HttpPostedFileBase> files)
    {
        foreach(var file in files)
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                if (fileName != null)
                {
                    var path = Path.Combine(Server.MapPath("~/Content/cfdis"), fileName);
                    file.SaveAs(path);

                    XmlDocument xDoc = new XmlDocument();
                    xDoc.Load(path);
                }
            }
        }


        return RedirectToAction("Index");
    }

0 个答案:

没有答案