0

I have a binary file that I wish to send over an ajax call back to a website. In order to do this I have the following code:

[Microsoft.AspNetCore.Mvc.Route("api/BIM/GetModifiedRevitFile")]    
public ActionResult GetModifiedRevitFile(string json)
{
    string tmppath = Path.GetTempPath();
     var fileVirtualPath = tmppath + "result.rvt";
    WorkItemHandler.ExecuteWorkItem(fileVirtualPath);
    var res= PhysicalFile(fileVirtualPath, "application/octet-stream", Path.GetFileName(fileVirtualPath));

        return res;

and the vue/javascript code:

AskToStoreFile: function (file, self) {
    alert("asking stuff")
    alert(file.responseText);
    var saveByteArray = (function () {
        var a = document.createElement("a");
        document.body.appendChild(a);
        a.style = "display: none";
        return function (data, name) {
            var blob = new Blob(data, { type: "octet/stream" }),
                url = window.URL.createObjectURL(blob);
            a.href = url;
            a.download = name;
            a.click();
            window.URL.revokeObjectURL(url);
        };
    }());

    saveByteArray([file.responseText], 'example.dat');
},

UploadLightsToServer: function() {
    var self = this;

    $.ajax({
        type: "GET",
        timeout: 12000000,
        url: 'api/BIM/GetModifiedRevitFile',
        contentType: "application/octet-stream"
        dataType: "application/octet-stream",
        success: function (data) {
            self.AskToStoreFile(data, self);
        },
        error: function (data) {
            self.AskToStoreFile(data,self)
            alert("failed to load data" + JSON.stringify(data));
        }
    });
},

StoreLightsInRevit: function() {
    alert("doing a revit");
    this.UploadLightsToServer();
},

Now this code sends the file in mostly correct, however a lot of binairy characters such as

ÐÏࡱá

becomes

��ࡱ�

I know this is probably some kind of encoding issue but I have no idea how to fix this.

Thijser
  • 2,625
  • 1
  • 36
  • 71
  • `contentType: "application/json",` Json is not binary data, and implicitly is utf-8 encoded. – Keith Oct 31 '18 at 14:39
  • @Keith I tried to change it to contentType: "application/octet-stream" but that had the same result. – Thijser Oct 31 '18 at 14:46
  • Also it appears the received data is a lot smaller than the 6mb that should have been send, is that just encoding making the compression a lot more efficient? – Thijser Oct 31 '18 at 14:48
  • You want to use `blob`, but jquery doesn't appear to support it directly, but there is a XHR callback you can patch into,.. have a look here -> https://stackoverflow.com/questions/17657184/using-jquerys-ajax-method-to-retrieve-images-as-a-blob – Keith Oct 31 '18 at 14:52
  • @Keith thanks that seems to work, would you be willing to put that down as an answer so I can accept it? – Thijser Nov 01 '18 at 08:11

1 Answers1

1

As pointed out in the comments, JQuery's ajax doesn't appear to have blob support as standard.

But it does allow you to modify the XHR request.

This link here shows more details -> Using jQuery's ajax method to retrieve images as a blob

Cutting and pasting a small bit here for simplicity.

jQuery.ajax({
  url:....,
  xhrFields:{
    responseType: 'blob'
  },
  ...
});
Keith
  • 22,005
  • 2
  • 27
  • 44