Save image with webservice

2

I already have a webservice for which I pass an image (Base64) and it saves on my server. So far it's working perfectly.

On my screen you have this code:

function salvar() {
        var dados = {};

        //Utilizar o toDataURL para converter em Base64
        var base64 = document.getElementById("myCanvas").toDataURL("image/png");

        dados.base64 = base64.substr(base64.indexOf(',') + 1, base64.length);

        var WPath = "face1";

        $.ajax({
            type: 'POST',
            //Chamar o webmethod SalvarImagem em webservice.asmx
            url: "SalvarImagem.asmx/SalvarImagemX",
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: JSON.stringify(dados, WPath),
            success: function (data) {
                alert(data.d);
            }
            , error: function (xmlHttpRequest, status, err) {
                alert("Ocorreu o seguinte erro:" + xmlHttpRequest.responseText)
            }
        });
    }

And my webservice looks like this:

public class SalvarImagem : System.Web.Services.WebService
{

    [WebMethod]
    public string SalvarImagemX(string base64, string WPath)
    {
           //MemoryStream com o base64 recebido por parâmetro
        using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64)))
        {
            //Criar um novo Bitmap baseado na MemoryStream
            using (Bitmap bmp = new Bitmap(ms))
            {
                //Local onde vamos salvar a imagem (raiz do site + /canvas.png)
                //string path = Server.MapPath("/" + WPath + "/canvas.png");
                string path = Server.MapPath("/face2/canvas.png");

                //Salvar a imagem no formato PNG
                bmp.Save(path, ImageFormat.Png);
            }
        }

        return "Imagem foi salva com sucesso";
    }
}
}

My problem is that: As I said it works while I only pass a paramenter which is the string DATA. But I need to pass another string containing the folder where the image should be saved ...

When I put the second parameter (WPath) I got to receive the error message below ... Can anyone tell me what's wrong .... ??

The following error occurred:

  

{"Message": "Invalid web service call, missing value for parameter:   \ u0027WPath \ u0027. "," StackTrace ":" at   System.Web.Script.Services.WebServiceMethodData.CallMethod (Object   target, IDictionary 2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary 2 parameters) \ r \ n at   System.Web.Script.Services.RestHandler.InvokeMethod (HttpContext   context, WebServiceMethodData methodData, IDictionary'2 rawParams) \ r \ n   at   System.Web.Script.Services.RestHandler.ExecuteWebServiceCall (HttpContext   context, WebServiceMethodData   methodData) "," ExceptionType ":" System.InvalidOperationException "}

    
asked by anonymous 17.04.2017 / 19:21

2 answers

1

It's giving you an error because that's not how you use JSON.stringify for what you're trying to do.

You have passed two parameters to it, here:

JSON.stringify(dados, WPath)

According to documentation , the second parameter of JSON.stringify is replacer :

  

replacer: A function that alters the behavior of the stringification   process, or an array of String and Number objects that serves as a   whitelist for selecting / filtering the properties of the value object   to be included in the JSON string.

In a free translation, replacer would be "A function that changes the behavior of the transformation process in string, or an array of String and Number that serve as a list to select and filter the properties of the object to be transformed in string ".

I was going to show you how to use the second parameter, but that was not the question, so I'll leave a possible solution for your AJAX. Change the code I mentioned above by the following:

data: '{"base64":"' + dados + '","WPath":"' + WPath + '"}'

I hope it helps, any questions put there in the comments

    
17.04.2017 / 19:30
0

Thank you Diego ...

I have a lot of experience with AJAX and I did what you suggested but it is giving a syntax error that I can not identify ... follow image ..

    
17.04.2017 / 20:03