如何从加载项 [任务窗格或其他方式] 在 Outlook 中保存文件? [英] How to save a file in Outlook from Add-in [taskpane or otherwise]?

查看:112
本文介绍了如何从加载项 [任务窗格或其他方式] 在 Outlook 中保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个 Office/Outlook 加载项,可以与我用户的电子邮件进行交互.

I have built an Office/Outlook add-in that interacts with my user's email messages.

我需要/要求为我的加载项用户构建/创建文件并将其保存到桌面.

I have the need/requirement to build/create and save a file to the desktop for my add-in user.

我尝试了各种方法,使用典型的网络 JavaScript 解决方案.这些解决方案仅在通过 OWA 使用加载项时才有效.这些解决方案还在 Safari、FF、Chrome 等中成功测试......但在 Outlook for Mac/Windows 中无法使用.

I have tried various methods with typical JavaScript solutions for the web. These solutions work only when the add-in is being used through OWA. These solutions also test successfully in Safari, FF, Chrome, etc ... but will not work in Outlook for Mac/Windows.

我一直在[未成功]搜索 OfficeJS API 文档,寻找一种可能的 API 方法来保存文件而不是用 JavaScript 编写自己的.

I have been searching [unsuccessfully,] the OfficeJS API documentation for a possible API method to save a file instead of writing my own in JavaScript.

这种东西存在吗?

Outlook 加载项是否可以为来自Outlook for Mac/Windows"的用户创建文件并将其保存到桌面?

Can an Outlook add-in create and save a file to the desktop for a user from "Outlook for Mac/Windows"?

虽然这里的文档(https://docs.microsoft.com/en-us/office/dev/add-ins/concepts/browsers-used-by-office-web-add-ins) 建议(例如)Mac 版 Outlook 正在运行嵌入式 Safari 浏览器......它似乎是一个更加锁定的沙盒版 Safari.这让我得出了一个假设,再多的自定义 JavaScript 也无法让我实现我的功能.

While the documentation here (https://docs.microsoft.com/en-us/office/dev/add-ins/concepts/browsers-used-by-office-web-add-ins) suggests (for example) Outlook for Mac is running an embedded safari... it appears it is a much more locked down sandboxed version of Safari. Which leads me to the hypothesis, no amount of custom JavaScript will get me to my feature implementation.

注意:WRT 到实现我自己的 JS 来下载文件(仍然不成功,也许某个地方存在有效的 hack?)

Note: WRT to implementing my own JS to download a file (still unsuccessful, maybe a working hack exists here somewhere?)

// testing with a simple test image though will need to support more than images
const testBase64Image = '<insert_base64_string_of_a_jpg_image>';

downloadTempFile1(): void {
  const newFileName = 'test_download_image.jpg';
  const a = document.createElement("a"); // Create <a>
  a.target = "_blank"; // open in new tab
  // need special handling of certain content types?
  a.href = "data:octet-stream;base64," + testBase64Image; // Base64 Goes here
  a.download = newFileName; //File name Here
  a.click(); //Download file
}

downloadTempFile2(): void {
  window.location.href = 'data:application/octet-stream;base64,' + testBase64Image;
}

downloadTempFile3(): void {
  const blob = this.b64toBlob(testBase64Image, 'octet-stream');
  const blobUrl = URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.download = 'test_download_image3.jpg';
  link.target = '_blank';
  link.href = URL.createObjectURL(blob);
  link.click();
}

b64toBlob = (b64Data, contentType='', sliceSize=512): Blob => {
  const byteCharacters = atob(b64Data);
  const byteArrays = [];
  for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    const slice = byteCharacters.slice(offset, offset + sliceSize);
    const byteNumbers = new Array(slice.length);
    for (let i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);
    }
    const byteArray = new Uint8Array(byteNumbers);
      byteArrays.push(byteArray);
  }
  const blob = new Blob(byteArrays, {type: contentType});
  return blob;
}

Edit2:某些客户端版本显然存在 Office.context.ui.openBrowswerWindow,但我能够以这种方式使用 window.open().

There exists Office.context.ui.openBrowswerWindow for some Client versions apparently, but I was able to use window.open() in this fashion.

openNewWindow(): void {
  const wnd = window.open('data:application/octet-stream;base64,' + base64Image);
  setTimeout(function() {
    wnd.close();
  }, 3000);
}

这将在 OWA 中下载文件,但仍不会在桌面客户端中下载文件.

This will download the file in OWA, but will still not download the file in the desktop client.

Edit3:我发现我需要将我的清单锁定到 Office SDK Mailbox 1.8(要求集).我还发现 Outlook for Mac 顶部有一个小切换按钮,可以在旧 UI 和新 UI 之间进行切换.新 UI 不支持 1.8.window.open 需要 1.8 才能正常运行,但我还没有能够使用 window.open 下载文件.我仍然无法使用 openBrowserWindowApi 1.1.

I was able to figure out that I needed to lock down my manifest to Office SDK Mailbox 1.8 (Requirements Set). I was also able to figure out that Outlook for Mac has a little toggle button in the top to switch from old to new UI and back. New UI does not support 1.8. 1.8 was required for window.open to operate correctly, but I have yet to be able to use window.open to download a file. I am still unable to use openBrowserWindowApi 1.1 yet as well.

Edit4:我决定尝试使用 Office.context.ui.displayDialogAsync() 并向子窗口发送消息.虽然 displayDialogAsync 会从桌面客户端打开一个新窗口,但它不会让我为用户将 base64 文件保存到桌面.

I decided to try using Office.context.ui.displayDialogAsync() and sending a message to the child window. While the displayDialogAsync will open a new window from the desktop client, it will not let me save a base64 file to the desktop for the user.

推荐答案

openBrowserWindow 是解决此问题的方法.(我们不能直接从 Sandboxed IE 或 Edge Process 下载文件).这应该会在默认浏览器中打开一个 html 页面,然后用户可以通过链接/等下载文件.

openBrowserWindow is the workaround for this. (We cannot download files directly from the Sandboxed IE or Edge Process). This should open an html page in the default browser, that the user can then download files via a link/etc.

https://docs.microsoft.com/en-us/javascript/api/office/office.ui?view=excel-js-preview#openBrowserWindow_url_

window.open() 的功能有所不同,具体取决于 IE 还是 Edge 是底层渲染引擎,这可能会因 Office 和 Windows 版本(即创建 OpenBrowserWindow 的乳清)而异.

window.open() functions differently depending on if IE or Edge is the underlying rendering engine which can vary based on Office and Windows Version, which is whey OpenBrowserWindow was created.

有关 IE/Edge WebView 的信息,请参阅此链接:https://developer.microsoft.com/en-us/office/blogs/microsoft-edge-webview-for-office-add-ins/

See this link for information about IE/Edge WebView: https://developer.microsoft.com/en-us/office/blogs/microsoft-edge-webview-for-office-add-ins/

这篇关于如何从加载项 [任务窗格或其他方式] 在 Outlook 中保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆