zlyboy / wx-formdata

在小程序中使用formdata上传数据,可实现多文件上传
291 stars 118 forks source link

是否可以用 TextEncoder 类去实现 toUtf8Bytes 函数? #11

Open yusifeng opened 1 year ago

yusifeng commented 1 year ago
function stringToUtf8(string) {
    let encoder = new TextEncoder();
    return encoder.encode(string);
}

用这个替换 toUtf8Bytes 方法我试了下也没有问题,是出于什么原因必须要自己实现这个方法呢?

zlyboy commented 1 year ago

你说的对,只是我当时写的时候没想到,现在也懒得改了,哈哈哈 ◕‿◕

lmj01 commented 8 months ago

我因为传的参数有点多,就说一堆数组的坐标值,会导致fixed maximum call stack size exceeded 就改成了使用TextEncoder对象来处理字符串的buffer,不使用for或...语法,使用TypedArray.prototype.set来减少遍历问题

但是真机测试存在问题!小程序本身不能使用

zlyboy commented 8 months ago

能举个例子不,传了多少参数,小程序的TextEncoder网上应该有解决方案的。你可以把这里的utf8转换方式拿过来试试看https://developer.mozilla.org/zh-CN/docs/Glossary/Base64

lmj01 commented 8 months ago

因为TextEncoder不稳定,接口上没有明确,之前好像开放出来,后来又不行了。

我出现fixed maximum call stack size exceeded应该是convert转换时造成的,有一个数组的字符串,数组里面的又是数组,每个坐标值。在text to buffer 之前就出现了问题。我切换了思路,使用TypedArray接口来复制数据,避免了这个问题。