How to send file or image by ajax and receive it in webservice asp.net

0

Good I am new in this of the programming and I have been looking for information by Internet already two days and I do not find the solution, would like that they help me with this code, since I am trying to send an image via ajax to asp.net server, and that I save the image on disk and get the path and then save it in a database, without using MVC.

'Customer side:     Upload Image

<script type="text/javascript">

    $("#btnCargar").click(function () {
        var form_data = new FormData();

        form_data.append("archivo", $('#fileEjemplo')[0].files[0]);

               //  formData.append("file", file);
        $.ajax({
            url: '<%=ResolveUrl("bproducto.aspx/subirarchivos")%>',
            type: 'POST',
            data: form_data,
           // cache: false,
            processData: false, // Don't process the files
            contentType: false, // Set content type to false as jQuery will tell the server its a query string request
            enctype: 'multipart/form-data',

            success: function (response) {
                var res = response.d;
                console.log(res);


            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var err = eval("(" + XMLHttpRequest.responseText + ")");
                alert("ojo" + err.Message)
                console.log("Ajax Error!");
            }
        });

    });


     </script>

server side [WebMethod]

    public static  string subirarchivos(HttpPostedFileBase archivo){

?????????  //no sé cómo recibirlo
}
    
asked by danilo 22.03.2018 в 18:20
source

1 answer

0

If the file is added to the multipart of the HTTP call "should" be read in Request.Files

Therefore, try:

[WebMethod]
public static string SubirArchivos(){
    var archivo = Request.Files["archivo"];
}
    
answered by 22.03.2018 в 18:28