I try to upload files to the asp .net mvc server

0

I need your help I'm starting with asp.net mvc, I need to upload files to the server, this is my html:

<form action="javascripts:;" method="post" enctype="multipart/form-data">

<input type="file" id="fileUpload" name="fileUpload" value="" />
<input type="button" id="btn-send-ajax" value="Enviar" />
<a class="btn btn-default btn-sm" name="btn-send-ajax" href="#" onclick="EditPost();" data-toggle="tooltip" title="Exportar"> <i class="dx-icon-save"></i></a>

<script type="text/javascript">

 $(document).ready(function () {

    $("#btn-send-ajax").click(function () {


       var selectFile = ($("#fileUpload"))[0].files[0];

        var dataString = new FormData();

        if (!selectFile) {
            alert("No se ha Cargado El Archivo (html)");
        }


        dataString.append("fileUpload", selectFile);
        $.ajax({
            url: '@Url.Action("LoadFileView","Home")',
            type: "Post",
            data: dataString,
            contentType: false,
            processData: false,
            async:false,
            success: function (data) {
                if (typeof (data.value) != "undefined") {
                    alert(data.Message);
                } else {
                    alert("Error No identificado");
                }
            },

            error: function (data) {

            }

        });
    });
});

this is my controller:

 public ActionResult LoadFileView(HttpPostedFileBase fileUpload) {
        try
        {
            string path = Server.MapPath("~/Content/Images");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            fileUpload.SaveAs(path + Path.GetFileName(fileUpload.FileName));
        }
        catch (Exception e)
        {
            return Json(new { Value = false, Message = e.Message }, JsonRequestBehavior.AllowGet);
        }

        return Json(new { Value = true, Message = "Subido Con Exito" }, JsonRequestBehavior.AllowGet);


    }

everything works fine, it reaches the controller, the line is executed:

 return Json(new { Value = true, Message = "Subido Con Exito" }, JsonRequestBehavior.AllowGet);

but in the view it shows me the message: "Unidentified Error" I get to create the folder but not the file. I need a Hand, please ...

    
asked by carlos 13.11.2018 в 20:57
source

2 answers

1

The problem is in the if statement, more precisely in the comparison if (typeof (data.value) != "undefined") .

undefined is a registered javascript word so it should not have quotes, but interpret it as a String

    
answered by 13.11.2018 в 21:04
0

already works, the first change is that

 if (typeof (data.Value) != "undefined") 

and another thing is in the controller, I was putting the wrong route:

[HttpPost]
    public ActionResult LoadFileView(HttpPostedFileBase fileUpload) {
        try
        {
            string path = Server.MapPath("~/Content/Images");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Server.MapPath("~/Content/Images/");
            fileUpload.SaveAs(path + Path.GetFileName(fileUpload.FileName));
        }
        catch (Exception e)
        {
            return Json(new { Value = false, Message = e.Message }, JsonRequestBehavior.AllowGet);
        }

        return Json(new { Value = true, Message = "Subido Con Exito" }, JsonRequestBehavior.AllowGet);


    }
    
answered by 13.11.2018 в 21:11