Type of data to receive file in Asp.net (C #) sent from JavaScript

2

I have a page made in HTML and JavaScript , in this I have a form with several inputs among them is a input file , and I send them to server through Ajax and JS .

On the server side I receive the data of inputs , but upon receiving the input file I get an error of data type is incorrect.

My question is: What kind of data do I have to use on the server side ( ASP.net and C # ) in order to receive the file correctly?

Here part of the code I have:

HTML

  <form id='frm1'>
     <input type='text' id='nombre'>
     <input type='file' id='balance'>
  </form>

JavaScript

fuction guardar(){
 var nombre = $('#nombre').val();
 var files = $("#balance").get(0).files;
 PageMethods.Guardar(nombre,files[0],guardar_Callback, Failed_CallBack,context);
}

On the server side, I have the following method:

[WebMethod()]
public static int guardar(string nombre, tipoArchivo balance){
//lo que haga con la informacion
}

As I comment with the exception of the type of data to receive the file, everything works fine.

    
asked by eezzekl 04.01.2017 в 18:18
source

2 answers

2

After much research and with the mentioned requirements of Using JavaScrip , PageMethods and WebMethod , I must say it is not possible to upload a file to server, due to the limitations of the technologies themselves and the way they work.

To be able to perform this action it is necessary to use a UpdatePanel with server-side controls, which is what I have done, as indicated @ weimar and as this link shows link

Or use MVC as a sample @Lazarok in your answer link

    
answered by 12.01.2017 / 22:26
source
1
  

I've attached a code using ASP.NET MVC 4, this is the structure of the project   

Body of the Index.cshtml file

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
    <input type="file" id="file" value="enviar" /><br />
    </div>

</body>

<script type="text/javascript">

    function ficheroSeleccionado(e) {
        if (e.target.files.length > 0) {
            subirFichero(e.target.files[0]);
        }
    }

    function subirFichero(file) {
        var xhr = new XMLHttpRequest();
        var formData = new FormData();
        formData.append("file", file);
        xhr.addEventListener("error", function (e) {
            alert("Error subiendo el archivo.");
        }, false);
        xhr.addEventListener("load", function (e) {
            //Codigo subido OK
            alert("fichero subido: " + e.target.statusMessage + "->" + e.target.statusText);
        });
        xhr.open("POST", "@Url.Action("UpLoad")", true);
        xhr.send(formData);
    }

        document.getElementById("file").addEventListener("change", ficheroSeleccionado, false);

</script>

</html>

Body of the file UpLoad.cshtml

    <!DOCTYPE html>

<html>
<head>
    <title>title</title>
</head>
<body>
    <div>
        OK
    </div>
</body>
</html>

Body of the IndexController.cs file

 using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace SubiendoFicheroAJAX_ASP.MET.Controllers
    {
        public class IndexController : Controller
        {
            //
            // GET: /Index/

            public ActionResult Index()
            {
                return View();
            }

            [HttpPost]
            public ActionResult UpLoad(string descripcion, HttpPostedFileBase file)
            {
                if (file!=null)
                file.SaveAs(Server.MapPath("~/" + file.FileName));
                return View();
            }

        }
    }
    
answered by 06.01.2017 в 17:38