nolanlawson / blob-util

Cross-browser utils for working with binary Blobs
https://nolanlawson.github.io/blob-util
Apache License 2.0
503 stars 45 forks source link

ajax example #58

Closed AlonsoK28 closed 5 years ago

AlonsoK28 commented 5 years ago

Do you have an Ajax example?

I have tried send the blob via ajax but wasn't possible

foto:()=> {

      var s = document.createElement("script");
      s.type = "text/javascript";
      s.src = "https://unpkg.com/blob-util/dist/blob-util.min.js";
      $("head").append(s);

        $("#_foto").attr("src", "http://localhost:3000/pruebas-ar-web/recursos/images/mac/imagen.jpg");

        var img = $(`#_foto`);
        blobUtil.imgSrcToBlob(img.attr("src")).then(function (blob) {

          var fd = new FormData();

          fd.append("fname", 'photo');
          fd.append("photo", blob, "photo.jpeg");

          $.ajax({
            url: "/AppMAC/UploadFoto",
            type: "POST",
            data: fd,
            processData: false,
            contentType: false,
            success: function (response) {
              console.log(response);
              return "listo>>>>>>>>>";
            },
            error: function (jqXHR, textStatus, errorMessage) {
              parent.postMessage('{"type":"alert","Error al enviar la imagen: ' + errorMessage + '"}', "*");
              console.log(errorMessage); // Optional
              return "error>>>>>>>>>>";
            }

          }).done(function (data) {
            console.log(data);
            $("#FotoApp").dialog("close");
            return "hecho>>>>>>>";
          });
        }).catch(function (err) {
          // image failed to load
        });
    }

Previously function only returns this

Promise {<pending>}__proto__: Promise[[PromiseStatus]]: "resolved"[[PromiseValue]]: undefined

nolanlawson commented 5 years ago

There are lots of ways to post binary data to a server; you may want to try asking this question on StackOverflow. I found a similar answer here: https://stackoverflow.com/a/28143262 Hope this helps!