在asp.net mvc中使用jquery ajax上传xml文件

时间:2022-04-23 03:20:58

I am trying to upload xml file using jquery ajax in asp.net mvc.

我试图在asp.net mvc中使用jquery ajax上传xml文件。

My view:

 @using (Html.BeginForm("XmlReader", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "xmlUploadForm", name = "xmlUploadForm" }))
            {
                <div>
                    <input type="file" name="xmlFile" id="xmlFile" >&nbsp;
                    <input type="button" value="Submit" class="btn btn-primary" id="Submit" />
                </div>


            }

Inside JS file:

里面的JS文件:

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

        $.ajax({
            type: "post",
            url: '@Url.Action("XmlReader", "Home")',
            datatype: "xml",
            data: data,
            success: function (result) {
                ...
                }
            }
        });
    });

My Controller:

public ActionResult XmlReader(HttpPostedFileBase xmlFile)
        {
             ...
        }

I tried var data=$("#xmlFile").val() to send the data that is not working.What should be the right way to send the data to controller.

我尝试了var data = $(“#xmlFile”)。val()来发送不工作的数据。应该是将数据发送到控制器的正确方法。

1 个解决方案

#1


0  

You can check the file details in the controller .

您可以在控制器中检查文件详细信息。

@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new { enctype = "multipart/form-data",id="frm1" }))
{
    <div>
        <input type="file" name="xmlFile" id="xmlFile" />
        <input type="submit" value="Submit" class="btn btn-primary" id="Submit" />
    </div>


}

<script type="text/javascript">


    $("#Submit").click(function (e) {
        var formData = new FormData($('#frm1').get(0));
        e.preventDefault();



        $.ajax({
            type: "post",
            url: '@Url.Action("FileUpload", "Home")',
            mimeType: "multipart/form-data",
            contentType: false,
            processData: false,
            data: formData,
            success: function (data) {
                alert(data.Result);
            }
        });
    });

</script>




[HttpPost]
    public ActionResult FileUpload()
    {
        var data = HttpContext.Request.Files["xmlFile"];

        return Json(new {Result="true" });
    }

#1


0  

You can check the file details in the controller .

您可以在控制器中检查文件详细信息。

@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new { enctype = "multipart/form-data",id="frm1" }))
{
    <div>
        <input type="file" name="xmlFile" id="xmlFile" />
        <input type="submit" value="Submit" class="btn btn-primary" id="Submit" />
    </div>


}

<script type="text/javascript">


    $("#Submit").click(function (e) {
        var formData = new FormData($('#frm1').get(0));
        e.preventDefault();



        $.ajax({
            type: "post",
            url: '@Url.Action("FileUpload", "Home")',
            mimeType: "multipart/form-data",
            contentType: false,
            processData: false,
            data: formData,
            success: function (data) {
                alert(data.Result);
            }
        });
    });

</script>




[HttpPost]
    public ActionResult FileUpload()
    {
        var data = HttpContext.Request.Files["xmlFile"];

        return Json(new {Result="true" });
    }