如何在 JavaScript 中使用 Base64String 值设置 Dynamics CRM/365 字段

How to set a Dynamics CRM/365 field with Base64String value in JavaScript

我想用文档的 base 64 字符串值设置 "base64string",然后我将使用该值并将文档加载到 Sharepoint(我已经有了用于此工作的 c# 代码控制台应用程序)。

我下面的代码好像不行,基本上没有设置值,字段base64string是多行,100万个字符。

<html>
<head>
  <meta charset="utf-8">
</head>
<body>
  Please select a file and then hit Evaluate:
  <br/>
  <input id="file" type="file" />
  <button id="button">Upload        
    <script>           
      document.getElementById('button').addEventListener('click', function() {
        var files = document.getElementById('file').files;
        if (files.length > 0) {
          getBase64(files[0]);
        }
      });

      function getBase64(file) {
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function () {        
          Xrm.Page.getAttribute("base64string").setValue(reader.result);
        };
        reader.onerror = function (error) {};
      }        
    </script>      
</body>
</html>

Xrm.Page.getAttribute("base64string")中,你确定base64string是一个字段名吗?如果它是一个自定义字段,它应该有一个前缀,例如 abc_base64string.

此外 HTML 网络资源无法直接访问 Xrm.Page

Reference other web resources from an HTML web resource.

An HTML web resource added to a form can’t use global objects defined by the JavaScript library loaded in the form. An HTML web resource may interact with the Xrm.Page or Xrm.Utility objects within the form by using parent.Xrm.Page or parent.Xrm.Utility, but global objects defined by form scripts won’t be accessible using the parent.

我相信您的代码应该更像这样:

reader.onload = function () {        
    parent.Xrm.Page.getAttribute("abc_base64string").setValue(reader.result);
};