0

If I post a PDF to my vendors API, they return me a .png file as a blob (see update 2 as I am now unsure if they are returning blob data).

I would like to push this into Azure Blob Storage. Using my code listed below, it pushes something in, but the file is corrupted. Example: downloading the .png from Azure Blob Storage and trying to open it with Paint gives the following error:

This is not a valid bitmap file, or its format is not currently supported.

I have verified that the image is sent to me correctly as the vendor is able to open the .png on their side. I am wondering if I need to convert this to base64 or save it to a local Web directory before uploading it to Azure Blob Storage.

Here is my Angular front end Controller that calls my Node/Express backend for uploading to Azure once it receives the returned "image":

$.ajax({
            url: 'http://myvendorsapi.net/uploadPDF,
            type: "POST",
            data: formdata,
            mimeType: "multipart/form-data",
            processData: false,
            contentType: false,
            crossDomain: true,
            success: function (result) {          
                var containerName = 'container1';
                var filename = 'Texture_0.png';
                var file = result;
                $http.post('/postAdvanced', { containerName: containerName, filename: filename, file: file }).success(function (data) {
                    console.log("success!");
                }, function (err) {
                    //console.log(err);
                });
            },
            error: function (error) {
                console.log("Something went wrong!");
            }
        })
    }

Here is my Node/Express backend that uploads the blob to Azure Blob Storage. It gives no error, but the file can't be opened/gives the error stated above when opened in Paint:

app.post('/postAdvanced', function (req, res, next) {
    var containerName = req.body.containerName;
    var filename = req.body.filename;
    var file = req.body.file;

    blobSvc.createBlockBlobFromText(containerName, filename, file, function (error, result, response) {
        if (!error) {
            res.send(result);
        }
        else {
            console.log(error);
        }
    });
})

Update 1: The answer provided here allows me to pass in the URL of the vendors API for some endpoints: Download file via Webservice and Push it to Azure Blob Storage via Node/Express

It works as it writes the file at the endpoint to a temp folder. In my current scenario, I upload a PDF file and it returns an image file that I need to upload to Azure Blob Storage. Is there a way to use the answer here, but adjust it for a file that I already have (since it is returned to me) versus file streaming from a URL?

Update 2: In console logging the returned "file", it looks like it may be data. I am not sure, it looks like this:

Console Logged Result Data

Is this actually data, and if so, how do I make this into a file for upload?

UPDATE 3:

Since it appears that jQuery AJAX can't manage binary returns. I am able to "open" the blob using XMLHTTPResponse as follows, but I can't seem to push this into Azure as it gives me the following error:

TypeError: must start with number, buffer, array or string

Here is my request. Note that the file opens properly:

var form = document.forms.namedItem("fileinfo");
form.addEventListener('submit', function (ev) {

 var oData = new FormData(form);

    var xhr = new XMLHttpRequest();
    xhr.responseType = "arraybuffer";

    xhr.open("POST", "http://myvendorsapi/Upload", true);
    xhr.onload = function (oEvent) {
        if (xhr.status == 200) {
            var blob = new Blob([xhr.response], { type: "image/png" });
            var objectUrl = URL.createObjectURL(blob);
            window.open(objectUrl);

            console.log(blob);
            var containerName = boxContainerName;
            var filename = 'Texture_0.png';

            $http.post('/postAdvanced', { containerName: containerName, filename: filename, file: blob }).success(function (data) {
                //console.log(data);
                console.log("success!");
            }, function (err) {
                //console.log(err);
            });

        } else {
            oOutput.innerHTML = "Error " + xhr.status + " occurred when trying to upload your file.<br \/>";
        }
    };

    xhr.send(oData);
    ev.preventDefault();
}, false);
Community
  • 1
  • 1
Kode
  • 3,073
  • 18
  • 74
  • 140
  • I don't understand the createBlockBlobFromText. If it's an image, shouldn't it be binary? – Robin Shahan - MSFT Oct 22 '15 at 04:17
  • That's probably it. Would that mean I need to stream the data in? If so, how would I adjust my Azure code? Is that CreateBlockFromStream? – Kode Oct 22 '15 at 04:52
  • I would try DownloadToStream. I don't know what blobSvc is in your code. Here's a reference to a bunch of code samples in C# that upload and download blobs (among other things) and see if this is helpful to you: [Blob Code Samples](http://justazure.com/azure-blob-storage-part-three-using-the-storage-client-library/) – Robin Shahan - MSFT Oct 23 '15 at 07:34
  • I am using Node/JavaScript via the guidance here: https://azure.microsoft.com/en-us/documentation/articles/storage-node's-how-to-use-blob-storage/ Thus far I can't get this to work/what is the equivalent of DownloadToStream in JavaScript? blobSvc is my variable that connects to Azure Blob Storage (see the link for an example as I am following the examples there). – Kode Oct 23 '15 at 13:16
  • You were right, I would try "CreateBlockBlobFromStream". – Robin Shahan - MSFT Oct 23 '15 at 16:38
  • Thanks @RobinShahan-MSFT. I tried that but it gave a "TypeError: stream.pause is not a function". Any clues what I am missing? – Kode Oct 23 '15 at 17:51
  • Since I don't know node, that's about as helpful as I can be. I'll pass this on to the Azure Storage team and see if someone there can help. – Robin Shahan - MSFT Oct 26 '15 at 07:16
  • Thanks @RobinShahan-MSFT. I suspect I am not passing all the correct variables to the Azure Blob Storage call for a stream to occur – Kode Oct 26 '15 at 12:38

3 Answers3

4

createBlockBlobFromText will work with either string or buffer. You might need a buffer to hold the binary content due to a known issue of jQuery.

For a workaround, there are several options:

Option 1: Reading binary filesusing jquery ajax

Option 2: Use native XMLHttpRequest

Option 3: Write frontend with Node as well and browserify it. Your frontend code may look like:

var request = require('request');

request.post('http://myvendorsapi.net/uploadPDF', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var formData = { 
      containerName: 'container1',
      filename: 'Texture_0.png',
      file: body
    };
    request.post({ uri: '/postAdvanced', formData: formData }, function optionalCallback(err, httpResponse, body) {
      if (err) {
        return console.error('upload failed:', err);
      }
      console.log('Upload successful!  Server responded with:', body);
    });
  } else {
    console.log('Get snapshot failed!');
  }
});

Then the backend code may look like:

app.post('/postAdvanced', function (req, res, next) {
  var containerName = req.body.containerName;
  var filename = req.body.filename;
  var file = req.body.file;

  if (!Buffer.isBuffer(file)) {
    // Convert 'file' to a binary buffer
  }

  var options = { contentType: 'image/png' };
  blobSvc.createBlockBlobFromText(containerName, filename, file, options, function (error, result, response) {
    if (!error) {
      res.send(result);
    } else {
      console.log(error);
    }
  });
})
Community
  • 1
  • 1
  • This states that the binary option is being deprecated and we should avoid using it: http://docs.nodejitsu.com/articles/advanced/buffers/how-to-use-buffers Any other ideas? – Kode Oct 26 '15 at 16:01
  • Additionally, it didn't work/produced the same issue. – Kode Oct 26 '15 at 17:19
  • 1
    Agree that using 'binary' encoding is not a perfect solution. Updated. – Yang Xia - Microsoft Oct 27 '15 at 02:05
  • Thanks for the attempt, but it still loaded a corrupt image. Would it be better to download the stream to a temp folder first and then load it in? If so, how would I achieve that? – Kode Oct 27 '15 at 02:30
  • I am noticing that the uploaded files Content Type is set to application/octet-stream instead of image/png. Is that something I can change on my side or something the vendor needs to change in the file they are sending? – Kode Oct 27 '15 at 03:00
  • 1
    Content type can be set within the options. Updated the code for it. – Yang Xia - Microsoft Oct 27 '15 at 13:19
  • Makes sense and it is showing as image/png but the actual file can't be opened and is giving the same error of "This is not a valid bitmap file, or its format is not currently supported". Any clue on how I could test the data returned by my vendor to see if its corrupt from them? – Kode Oct 27 '15 at 14:32
  • How about writing the content to a local file firstly to verify whether the data is corrupted? I've verified the above code works when the 'req.body.file' is a buffer. – Yang Xia - Microsoft Oct 28 '15 at 04:36
  • I have updated my post and found that it may not be a blob that is getting returned but actually data (Please see the image in my post). Given this, are there adjustments to code we should make? – Kode Oct 28 '15 at 17:26
  • 1
    It looks like a limitation in jQuery that it cannot give binary data as a response which makes "req.body.file" always a string . Please refer to http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/ or http://stackoverflow.com/questions/17657184/using-jquerys-ajax-method-to-retrieve-images-as-a-blob for a workaround. – Yang Xia - Microsoft Oct 29 '15 at 03:43
  • Is there a way to do this all with node where I POST and get the return data via node? – Kode Oct 29 '15 at 04:04
  • Or formidable. Do you have any code samples/links I could see... Or an update to your answer that has both posting a file and saving the return data to Azure? Thanks again for all the help – Kode Oct 29 '15 at 12:11
  • Tried the workaround, but it didn't work. How can I post formdata using Node and get the response into Azure blob storage? It appears that the data may need to be buffered before hitting Azure storage, or streamed in. After a week of testing, I am out of ideas. – Kode Oct 30 '15 at 05:03
1

Below I have the code to upload the image as binary in angular using FormData.
The server code will be the code to handle a regular file upload via a form.

var form = document.forms.namedItem("fileinfo");
form.addEventListener('submit', function (ev) {

var oData = new FormData(form);

var xhr = new XMLHttpRequest();
xhr.responseType = "arraybuffer";

xhr.open("POST", "http://vendorapi.net/Upload", true);
xhr.onload = function (oEvent) {
    if (xhr.status == 200) {
        var blob = new Blob([xhr.response], { type: "image/png" });
        //var objectUrl = URL.createObjectURL(blob);
        //window.open(objectUrl);
        //console.log(blob);
        var formData = new FormData()
        formData.append('file', blob);
        formData.append('containerName', boxContainerName);
        formData.append('filename', 'Texture_0.png');

        $http.post('/postAdvancedTest', formData, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        }).success(function (data) {
            //console.log(data);
            console.log("success!");

            // Clear previous 3D render
            $('#webGL-container').empty();

            // Generated new 3D render
            $scope.generate3D();
        }, function (err) {
            //console.log(err);
        });
    } else {
        oOutput.innerHTML = "Error " + xhr.status + " occurred when trying to upload your file.<br \/>";
    }
};

xhr.send(oData);
ev.preventDefault();
}, false);
Musa
  • 96,336
  • 17
  • 118
  • 137
  • Any ideas on how this could all be handled server side in Node (both the posting and the return) without routing me to a different page once the process is complete? – Kode Nov 04 '15 at 14:59
  • 1
    I'm pretty green when it comes to node.js but you can check `req.files` for the file if you're on express.js < 4. If you're on version 4 or above you can use something like https://www.npmjs.com/package/multer to get the file. – Musa Nov 04 '15 at 15:17
0

I have solved the issue (thanks to Yang's input as well). I needed to base64 encode the data on the client side before passing it to node to decode to a file. I needed to use XMLHTTPRequest to get binary data properly, as jQuery AJAX appears to have an issue with returning (see here: http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/).

Here is my front end:

var form = document.forms.namedItem("fileinfo");
form.addEventListener('submit', function (ev) {

var oData = new FormData(form);

var xhr = new XMLHttpRequest();
xhr.responseType = "arraybuffer";

xhr.open("POST", "http://vendorapi.net/Upload", true);
xhr.onload = function (oEvent) {
    if (xhr.status == 200) {
        var blob = new Blob([xhr.response], { type: "image/png" });
        //var objectUrl = URL.createObjectURL(blob);
        //window.open(objectUrl);
        console.log(blob);
        var blobToBase64 = function(blob, cb) {
            var reader = new FileReader();
            reader.onload = function() {
                var dataUrl = reader.result;
                var base64 = dataUrl.split(',')[1];
                cb(base64);
            };
            reader.readAsDataURL(blob);
        };

        blobToBase64(blob, function(base64){ // encode
            var update = {'blob': base64};
            var containerName = boxContainerName;
            var filename = 'Texture_0.png';

            $http.post('/postAdvancedTest', { containerName: containerName, filename: filename, file: base64}).success(function (data) {
                //console.log(data);
                console.log("success!");

                // Clear previous 3D render
                $('#webGL-container').empty();

                // Generated new 3D render
                $scope.generate3D();
            }, function (err) {
                //console.log(err);
            });
        })
    } else {
        oOutput.innerHTML = "Error " + xhr.status + " occurred when trying to upload your file.<br \/>";
    }
};

xhr.send(oData);
ev.preventDefault();
}, false);

Node Backend:

app.post('/postAdvancedTest', function (req, res) {
var containerName = req.body.containerName
var filename = req.body.filename;
var file = req.body.file;
var buf = new Buffer(file, 'base64'); // decode

var tmpBasePath = 'upload/'; //this folder is to save files download from vendor URL, and should be created in the root directory previously.
var tmpFolder = tmpBasePath + containerName + '/';

// Create unique temp directory to store files
mkdirp(tmpFolder, function (err) {
    if (err) console.error(err)
    else console.log('Directory Created')
});

// This is the location of download files, e.g. 'upload/Texture_0.png'
var tmpFileSavedLocation = tmpFolder + filename;

fs.writeFile(tmpFileSavedLocation, buf, function (err) {
    if (err) {
        console.log("err", err);
    } else {
        //return res.json({ 'status': 'success' });
        blobSvc.createBlockBlobFromLocalFile(containerName, filename, tmpFileSavedLocation, function (error, result, response) {
            if (!error) {
                console.log("Uploaded" + result);

                res.send(containerName);
            }
            else {
                console.log(error);
            }
        });
    }
}) 
})
Kode
  • 3,073
  • 18
  • 74
  • 140
  • I'm curious to know why you couldn't use a form data object to send the file blob as binary rather that converting it text. I'd be surprised id angular couldn't handle a binary request. – Musa Nov 02 '15 at 05:37
  • I had tried jquery before learning of the limitation with binary. At that point, I went straight for xmlhttprequest and not formdata via angular – Kode Nov 02 '15 at 12:59