Get the file of an input type file

1

Hi, I want to get the file from an input file. I do not want his name, nor his properties if not the file as such, and then upload it to a repository on my server. But I think I'm doing something wrong with my Js . I would be infinitely grateful for your help.

    function tryme() {

        var file = $('#fileinput').prop("files")[0];

        $.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            url: '@Url.Action("CreateRequest", "Home")',
            data: "{'File':'" + file + "'}",
            success: function (response) {
                alert("at least Ajax looks fine");
            },
            Error: function (textMsg) {

                alert("something got wrong");
            }
        });
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <input type="file" id="fileinput"   />
<input type="submit"  onclick="tryme()" value="click" />

</body>
</html>

It's pretty simple, it pulls undefinded or [object FileList] .
I would like to know if this [object FileList] can I use it to upload it to the database?

   
    public JsonResult  CreateRequest(FileInfo format )
    {

        string cs = "Data Source=DMX87025;Initial Catalog=DB_PCC;Integrated Security=True";
        string sql = "INSERT INTO [DB_PCC].[dbo].[Testsoru]" +
            "([Format])" +
            "VALUES" +
            "(@Format)";

        

        using (SqlConnection con= new SqlConnection(cs) ) {
            SqlCommand cmd = new SqlCommand(sql,con);
            
            cmd.Parameters.Add("@Format", SqlDbType.VarChar).Value = format;

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

       
        return Json(new { success = true, message = "Success:D" }, JsonRequestBehavior.AllowGet);


        // End CreateRequest
    }
    
asked by E.Rawrdríguez.Ophanim 24.10.2017 в 17:27
source

1 answer

1

I assume that you have a upload button whose id is upload_button and from there I assemble the following example, this code is on the client side, on the server side I imagine you have no problem dealing with the uploaded files. :)

$('#upload_button').on('click', function(){
    //Obtengo el fichero que va a ser subido
    var dato_archivo = $('#fileinput').prop("files")[0];
    //Creo un dato de formulario para pasarlo en el ajax
    var form_data = new FormData();
    //Añado los datos del fichero que voy a subir
    //En el lado del servidor puede obtener el archivo a traves de $_FILES["file"]
    form_data.append("file", dato_archivo);
    //Realizo el ajax
    $.ajax({
        //La url que se encargara de procesar la subida del archivo
        url: 'tu_pagina.php',
        //El tipo de respuesta que me devolverá la página en mi caso será un texto indicando el estado de la subida
        dataType: 'text',
        processData: false,
        //El dato pasado a la solicitud
        data: form_data,
        //El tipo que será la solicitud
        type: 'post',
        //Si la operación tiene éxito...
        success: function(respuesta){
           alert(respuesta);
        },
        //Si la operación tiene un error
        error: function(){
            alert("Ha ocurrido un error");
        }
    });
});
    
answered by 24.10.2017 / 18:08
source