Back to Uni App

Base64

docs/api/base64.md

2.3.36.5 KB
Original Source
<!-- ## uni.base64ToArrayBuffer(base64) @base64toarraybuffer -->

::: sourceCode

uni.base64ToArrayBuffer(base64) @base64toarraybuffer

GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-base64ToArrayBuffer

GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-base64ToArrayBuffer

:::

将 Base64 字符串转成 ArrayBuffer 对象

base64ToArrayBuffer 兼容性

Web微信小程序AndroidiOSHarmonyOSHarmonyOS(Vapor)
4.04.414.514.614.615.0

参数

名称类型必填默认值兼容性描述
base64string-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -

返回值

类型
ArrayBuffer

参见

<!-- ## uni.arrayBufferToBase64(arrayBuffer) @arraybuffertobase64 -->

::: sourceCode

uni.arrayBufferToBase64(arrayBuffer) @arraybuffertobase64

GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-arrayBufferToBase64

GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-arrayBufferToBase64

:::

将 ArrayBuffer 对象转成 Base64 字符串

arrayBufferToBase64 兼容性

Web微信小程序AndroidiOSHarmonyOSHarmonyOS(Vapor)
4.04.414.514.614.615.0

参数

名称类型必填默认值兼容性描述
arrayBufferArrayBuffer-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -

返回值

类型
string

参见

示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见 ::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/API/base64/base64

appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/base64/base64

示例

vue
<template>
	<view>
		<view>
			<button class="btnstyle" type="primary" @tap="arrayBufferToBase64"> arrayBufferToBase64 </button>
			<button class="btnstyle" type="primary" @tap="base64ToArrayBuffer"> base64ToArrayBuffer </button>
		</view>
	</view>
</template>

<script setup lang="uts">
	type DataType = {
		arrayBufferToBase64Res: string;
		base64ToArrayBufferRes: string;
	}

	const data = reactive({
		arrayBufferToBase64Res: '',
		base64ToArrayBufferRes: ''
	} as DataType)

	const arrayBufferToBase64 = () => {
		const arraybuffer = new ArrayBuffer(3)
		const uint8 = new Uint8Array(arraybuffer)
		uint8.set([11, 22, 33])
		const base64 = uni.arrayBufferToBase64(arraybuffer)
		console.log(base64) //CxYh
		data.arrayBufferToBase64Res = base64
		uni.showToast({
			title: data.arrayBufferToBase64Res
		})
	}

	const base64ToArrayBuffer = () => {
		const base64 = 'CxYh'
		const arrayBuffer = uni.base64ToArrayBuffer(base64)
		const uint8 = new Uint8Array(arrayBuffer)
		console.log(uint8.toString())//11, 22, 33
		data.base64ToArrayBufferRes = uint8.toString()
		uni.showToast({
			title: data.base64ToArrayBufferRes
		})
	}

	defineExpose({
		data,
		arrayBufferToBase64,
		base64ToArrayBuffer
	})
</script>

<style>
	.logo {
		height: 100px;
		width: 100px;
		margin: 100px auto 25px auto;
	}

	.title {
		font-size: 18px;
		color: #8f8f94;
		text-align: center;
	}

  .btnstyle {
  	margin: 4px;
  }
</style>

:::

通用类型

GeneralCallbackResult

名称类型必备默认值兼容性描述
errMsgstring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -错误信息