Skip to content

Commit cdab4c1

Browse files
luka-mimisei-luka
andauthored
fix(editor): Fix the issue that the content of json, html, csv, md, txt, and css files contained garbled Chinese characters when clicking the view button (#16118)
Co-authored-by: luka <[email protected]>
1 parent 0cfe6ee commit cdab4c1

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

packages/frontend/editor-ui/src/components/BinaryDataDisplayEmbed.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { ref, onMounted, computed } from 'vue';
33
import { useWorkflowsStore } from '@/stores/workflows.store';
44
import type { IBinaryData } from 'n8n-workflow';
5-
import { jsonParse } from 'n8n-workflow';
5+
import { jsonParse, base64DecodeUTF8 } from 'n8n-workflow';
66
import VueJsonPretty from 'vue-json-pretty';
77
import RunDataHtml from '@/components/RunDataHtml.vue';
88
import { useI18n } from '@n8n/i18n';
@@ -28,12 +28,13 @@ onMounted(async () => {
2828
const { id, data: binaryData, fileName, fileType, mimeType } = props.binaryData;
2929
const isJSONData = fileType === 'json';
3030
const isHTMLData = fileType === 'html';
31-
3231
if (!id) {
3332
if (isJSONData || isHTMLData) {
34-
data.value = jsonParse(atob(binaryData));
33+
data.value = isJSONData
34+
? jsonParse(base64DecodeUTF8(binaryData))
35+
: base64DecodeUTF8(binaryData);
3536
} else {
36-
embedSource.value = 'data:' + mimeType + ';base64,' + binaryData;
37+
embedSource.value = `data:${mimeType};charset=utf-8;base64,${binaryData}`;
3738
}
3839
} else {
3940
try {

packages/workflow/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export {
2727
isObjectEmpty,
2828
deepCopy,
2929
jsonParse,
30+
base64DecodeUTF8,
3031
jsonStringify,
3132
replaceCircularReferences,
3233
sleep,

packages/workflow/src/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,28 @@ type JSONStringifyOptions = {
155155
replaceCircularRefs?: boolean;
156156
};
157157

158+
/**
159+
* Decodes a Base64 string with proper UTF-8 character handling.
160+
*
161+
* @param str - The Base64 string to decode
162+
* @returns The decoded UTF-8 string
163+
*/
164+
export const base64DecodeUTF8 = (str: string): string => {
165+
try {
166+
// Use modern TextDecoder for proper UTF-8 handling
167+
const bytes = new Uint8Array(
168+
atob(str)
169+
.split('')
170+
.map((char) => char.charCodeAt(0)),
171+
);
172+
return new TextDecoder('utf-8').decode(bytes);
173+
} catch (error) {
174+
// Fallback method for older browsers
175+
console.warn('TextDecoder not available, using fallback method');
176+
return atob(str);
177+
}
178+
};
179+
158180
export const replaceCircularReferences = <T>(value: T, knownObjects = new WeakSet()): T => {
159181
if (typeof value !== 'object' || value === null || value instanceof RegExp) return value;
160182
if ('toJSON' in value && typeof value.toJSON === 'function') return value.toJSON() as T;

0 commit comments

Comments
 (0)