“This attachment can not be opened on your device”–error while trying to open a file using Xrm.Navigation.openFile

This is an error I was facing while trying to download file using Xrm.Navigation.openFile. I was trying on unified interface on web browser. Tried on all browsers and I was getting this error.

Below is my code which I was trying.

var file = {
    fileContent: “YWJjZA==”, //Contents of the file.
    fileName: “test.txt”, //Name of the file.
    mimeType: “text/plain” //MIME type of the file.
};

Xrm.Navigation.openFile(file, 2).then(
    function (s) {
       console.log(“File downloaded successfully.”);
    }, function (e) {
       console.log(e.message);
    });

And I was getting this error – “This attachment cannot be opened on your device”.

And then I went back to Microsoft Docs – Read carefully and came across this.

image

openFileOptions is an object. Where we need to specify openMode.

So all I did was to create openFileOptions object and set the value of openMode property.

var openFileOptions = {openMode: 2};

var file = {
    fileContent: “YWJjZA==”, //Contents of the file.
    fileName: “test.txt”, //Name of the file.
    mimeType: “text/plain” //MIME type of the file.
};

Xrm.Navigation.openFile(file, openFileOptions).then(
    function (s) {
       console.log(“File downloaded successfully.”);
    }, function (e) {
       console.log(e.message);
    });

And voila! It worked. Small trick did it.

Hope this helps!

Debajit Dutta

(Business solutions MVP)

For training and consulting, please write to us at info@xrmforyou.com


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading