这是我对问题的解决方案。
除了 jQuery,抓取FileSaver.js、Blob.js和base64-binary.js。
如下创建您自己的 download.js 文件,并将 addRequestDownloadLink 函数添加到表单的 OnLoad 事件中。
由于原始许可证数据是二进制的,因此您需要使用 Base64-binary.js 而不是 atob()。
function addRequestDownloadLink() {
$('#my_download').append('<div id="my_download_downloadcontainer"><a href="#" id="my_download_requestdownload">Request download of license file</a></div>');
}
$('body').on('click', '#my_download_downloadlink', function() {
var str_decoded = Base64Binary.decode($(this).attr('data-license'));
var blob = new Blob([str_decoded], {type: "license/binary"});
saveAs(blob, $(this).attr('data-filename'));
});
$('body').on('click', '#my_download_requestdownload', function() {
var guid = Xrm.Page.data.entity.getId();
guid = guid.replace("{", ""); guid = guid.replace("}", "");
var filename = Xrm.Page.getAttribute("my_name").getValue() + ".dat";
var organization = Xrm.Page.context.getOrgUniqueName();
var entity = "my_license";
var select = "?$select=my_DataSigned"
var oDataSelect = "/" + organization + "/XRMServices/2011/OrganizationData.svc/" + entity + "Set(guid'" + guid + "')" + select;
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: oDataSelect,
beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
success: function (data, textStatus, XmlHttpRequest) {
if (data.d.my_DataSigned != null) {
$('#my_download_downloadcontainer').html('<a href="#" id="my_download_downloadlink" data-license="' + data.d.my_DataSigned + '" data-filename="' + filename + '">Download license file!</span>');
}
else {
alert("No license found!");
}
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
}
});
});
太棒了!希望这会帮助有同样问题的人。