若要在 Power Automate 中將影像轉換成 Base64,請先在本機將檔案編碼為標準的 RFC 4648 字串,再把產生的文字貼到 Compose 動作、已初始化的變數或運算式裡——Power Automate 將 Base64 視為純文字,並讓你從任何下游步驟引用它。Power Automate Desktop 內建原生動作可進行此轉換,但雲端流程並沒有,因此當 SharePoint 檔案、表單附件或 HTTP 回應帶有 PNG、JPEG、GIF 或 WebP 時,流程通常需要在平台外建立一個乾淨的 Base64 承載內容,再放入變數中。若不撰寫程式碼或上傳影像,最快的產生方式是使用瀏覽器工具來解碼位元組、驗證容器,並輸出純 Base64 或帶有正確 MIME 類型的完整資料 URL。一旦複製好文字,Compose 或設定變數步驟會將其保存供流程的其餘部分使用,而像 base64ToBinary()、concat() 或 variables() 等運算式可以將它塑形以供 Outlook、Adaptive Cards、Dataverse 或外部 HTTP 端點使用。其結果是一個對流程友善的文字值,能通過執行歷程傳遞、在重試時存活下來,且不依賴 CDN 或臨時 URL。

how to convert image to base64 in power automate
如何在 Power Automate 中將影像轉換成 Base64

需要影像 Base64 的常見 Power Automate 情境

為什麼 Power Automate 流程需要 Base64 字串?大多數觸發條件和動作會回傳檔案識別碼(SharePoint 路徑、附件 ID、Dataverse 資料列)或二進位 blob,但下游消費者通常希望影像以內嵌文字呈現。最常出現的情境包括:

  • 將標誌、簽名或圖表直接內嵌在 Outlook 電子郵件本文中,使郵件無需外部影像請求即可呈現
  • 透過 HTTP 動作將影像傳遞給接受 JSON 或帶有 Base64 欄位的 form-data 的 API
  • 將小型參考影像儲存在 Dataverse 文字欄位、SharePoint 清單欄或 Excel Online 表格中
  • 透過 url 屬性提供 Adaptive Card 影像,其中資料 URL 消除了對公開 CDN 的需求
  • 建構 Power Automate 能在測試期間確定性重播的 JSON 固定資料與模擬資料
  • 將檔案內容傳送給偏好文字傳輸而非 multipart 上傳負擔的服務

每種情境都有相同的根本需求:一個乾淨的 RFC 4648 Base64 字串,承載原始影像的確切位元組,並搭配位元組實際代表的 MIME 類型。Power Automate 不會自行產生該字串,這正是專用本機編碼器所填補的落差。

為 Power Automate 產生有效的 Base64 字串

在 Power Automate 中將影像轉換成 Base64 的確切方式,取決於影像的來源以及 Base64 的使用位置。雲端流程和桌面流程採行不同的路徑,因此將任務拆分為一次性的本機編碼步驟,以及一系列在執行過程中傳遞字串的 Power Automate 步驟會有所幫助。

對於影像檔案位於平台外的雲端流程:

  1. 在任何現代瀏覽器分頁中開啟 Image to Base64 Converter,並載入單一的 PNG、JPEG、GIF 或 WebP 檔案。
  2. 若下游步驟僅需要編碼後的字元,請選擇純 Base64;若消費者預期完整 data:image/...;base64,... 前綴以便貼入 HTML 或 Adaptive Card,則選擇資料 URL。
  3. 點擊 Convert,並確認預覽以實際解碼的像素尺寸顯示預期的影像。
  4. 將完整的輸出文字複製到剪貼簿。
  5. 在 Power Automate 設計工具中,新增一個 Compose 動作(或 Initialize Variable),例如命名為 ImageBase64,並將文字貼上作為輸入。
  6. 從任何後續步驟使用動態內容選擇器或運算式中的 variables('ImageBase64') 來引用該值。

對於 Power Automate Desktop 流程,相同的字串仍然適用,但通常可以完全跳過瀏覽器步驟。使用 Get files from folder 動作,或以檔案輸入觸發流程,透過 Read file from disk 動作將檔案放入二進位變數,在該變數上套用 Cryptography 群組中的 Encode to Base64 string 動作,並將結果儲存在文字變數中,供子流程、webhook 或剪貼簿動作使用。

當影像已作為檔案內容 blob 存在流程中時(例如 SharePoint Get file content 動作的本文),Power Automate 會回傳二進位而非 Base64。請使用 base64() 運算式包裝該二進位,以便在不離開設計工具的情況下取得內嵌字串:

  • base64(triggerBody()?['file']) 用於檔案位於觸發條件承載上的情況
  • base64(body('Get_attachment')?['contentBytes']) 用於檔案作為附件傳入的情況
  • base64ToBinary(...) 用於下游動作預期取回原始位元組的情況
  • base64ToString(...) 用於你只需要在編碼後檢查文字的情況

這些運算式證實了流程將 Base64 文字視為影像資料的標準傳輸格式,這就是為什麼從本機編碼器複製的乾淨、格式正確的字串可以直接放入,無需進一步轉換。

使用瀏覽器工具在本機編碼影像檔案

使用 Image to Base64 Converter 編碼影像是個簡短、可重複的過程,整個流程都在裝置上執行。瀏覽器會讀取檔案、驗證位元組容器、解碼以取得真實預覽,然後才輸出編碼後的字串。

  1. 開啟 Image to Base64 Converter 頁面,從裝置中選擇單一的 PNG、JPEG、GIF 或 WebP 檔案。
  2. 選擇輸出模式:若下游步驟僅需要編碼後的字元,請選擇純 Base64;若消費者預期完整 data:image/...;base64,... 前綴,則選擇影像資料 URL。
  3. 點擊 Convert。工具會檢查檔案大小、驗證結構容器(PNG 簽章加上 IHDR 加上 IEND、JPEG SOI 加上 EOI、GIF 標頭加上結尾,或 WebP RIFF 加上 WEBP 加上有界限的第一個區塊),然後透過瀏覽器的影像解碼器解碼影像。
  4. 確認偵測到的位元組格式、實際解碼的像素尺寸、檔案大小以及預覽縮圖。縮圖僅作為版面配置限制;編碼後的承載內容會精確保留原始位元組。
  5. 複製完整的輸出。編碼遵循 RFC 4648 的 Base-N 字母表,包含大小寫字母、數字、加號和斜線,當最後一組少於三個位元組時,會有一個或兩個結尾等號。複製動作受到產生保護,因此稍後的轉換無法用過時資料覆寫成功的剪貼簿寫入。

輸入上限剛好為 5 MiB(5,242,880 位元組);超過一個位元組的檔案會在讀取其本文之前遭到拒絕,且之後會再次檢查實際的 ArrayBuffer 長度。該位元組預算的最大 Base64 字串長度為 6,990,508 個字元,而最長支援的資料 URL 可容納 6,990,531 個字元——任何超出都會回傳明確的錯誤,且不會產生部分字串。解碼後的影像也必須符合每邊 20,000 像素、總面積 40,000,000 像素的限制,因為小型壓縮檔案在解碼為原始像素記憶體後可能大幅膨脹。

將編碼後的字串貼入 Power Automate

字串進入剪貼簿後,請將其貼入需要它的 Power Automate 步驟。確切的放置位置取決於下游動作的預期:

  • Compose 或 Initialize Variable:將 Base64 文字貼入輸入框。Power Automate 會將其儲存為字串,你可以透過動態內容選擇器或運算式中的 variables('ImageBase64') 來引用它。
  • Outlook Send Email (V2) HTML 內文:內嵌建構一個 img 標籤,例如 <img src="data:image/png;base64,@{variables('ImageBase64')}" />,郵件即可在沒有任何公開 CDN 的情況下呈現影像。
  • HTTP 動作內文:將純 Base64 貼入 JSON 屬性,例如 "imageBase64": "@{variables('ImageBase64')}",並將要求標頭 Content-Type 設定為 application/json。有些 API 偏好資料 URL 前綴;有些則偏好純文字——請依照端點文件的規範。
  • Adaptive Card:將影像 url 屬性設定為完整的資料 URL,包含前綴。Adaptive Card 會直接呈現內嵌的影像,因此無需將檔案託管在 SharePoint 或任何其他位置。
  • Dataverse 或 SharePoint 清單文字欄:將純 Base64 字串貼入該欄位。文字欄位非常適合容納在幾百 KB 以下的小型參考圖示、簽名和縮圖大小的影像。

如果流程從其他系統接收 Base64 文字並需要重建影像,則反向流程描述於如何在 Power Automate 中將 Base64 轉回影像。在設計工具中,當來源為二進位時,請使用 base64() 或 base64ToBinary() 包裝任何貼上的值;當目標需要附加或去除資料 URL 前綴時,請使用 concat() 或 replace() 等字串函式。Power Automate 會同步評估這些運算式,因此編碼後的字串能在多個步驟間傳遞,無需暫存檔案或外部服務。

Supported Formats, Hard Limits, and Common Pitfalls

The Image to Base64 Converter supports exactly four byte containers. Anything else—including SVG, AVIF, HEIC, BMP, TIFF, PDF, and arbitrary text—is outside scope even if the browser or the file extension calls it an image. Encode those containers with a different converter first, then run the result through this tool only if the output is already PNG, JPEG, GIF, or WebP.

Format Detected from bytes Data URL MIME
PNG 8-byte signature, valid IHDR chunk, terminal IEND image/png
JPEG SOI marker, following marker, terminal EOI image/jpeg
GIF GIF87a or GIF89a header, logical screen descriptor, trailer image/gif
WebP RIFF length matches file size, WEBP FourCC, bounded VP8, VP8L, or VP8X first chunk image/webp

The MIME in data URL mode is taken from the verified byte container, never from the filename or the OS-declared MIME. A PNG renamed photo.jpg still produces data:image/png;base64,..., which is exactly what Power Automate and strict image consumers want. Limits and trade-offs to keep in mind:

  • The hard input cap is 5 MiB (5,242,880 bytes). One byte beyond is rejected before reading, and the actual ArrayBuffer length is checked again afterwards.
  • The maximum Base64 length for that input is 6,990,508 characters, and the longest data URL brings the total to 6,990,531 characters. Anything beyond returns an explicit error and no partial string.
  • Decoded dimensions are bounded by 20,000 pixels per edge and 40,000,000 pixels of total area. A corrupted or oversized image is rejected rather than producing a misleading result.
  • Base64 inflates transport size by roughly one third before any data URL prefix. For ordinary website delivery, host the image file and reference it normally; reserve Base64 for cases where a text representation is genuinely required, such as API payloads, JSON fixtures, email templates, offline prototypes, test data, and debugging flows.
  • Conversion preserves the original bytes. The tool does not draw to canvas, resize, recompress, recolor, flatten transparency, select a GIF frame, remove metadata, or repair damage. Animated GIF and WebP bytes stay animated in the Base64 payload as long as the downstream consumer supports animation.

For smaller output before encoding, run the original image through the Image Compressor. For new pixel dimensions, run it through the Image Resizer first. Doing these tasks separately keeps the Base64 encoding predictable, because the string is a literal representation of the bytes the converter sees.

For a deeper look, see Base64 to Image Converter Alternative: Strict and Local.