Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从base64节点js转换后.txt文件开头出现的一些随机字符_Javascript_Node.js - Fatal编程技术网

Javascript 从base64节点js转换后.txt文件开头出现的一些随机字符

Javascript 从base64节点js转换后.txt文件开头出现的一些随机字符,javascript,node.js,Javascript,Node.js,因此,我将一个.txt文件放入一个上传程序,该上传程序将其转换为base64数据,如下所示: const {getRootProps, getInputProps} = useDropzone({ onDrop: async acceptedFiles => { let font = ''; // its not actually a font just reusing some code i'll change it later its a .t

因此,我将一个.txt文件放入一个上传程序,该上传程序将其转换为base64数据,如下所示:

 const {getRootProps, getInputProps} = useDropzone({
        onDrop: async acceptedFiles => {
            let font = ''; // its not actually a font just reusing some code i'll change it later its a .txt file so wherever you see font assume its NOT a font.
            let reader = new FileReader();

            let filename = acceptedFiles[0].name.split(".")[0];
            console.log(filename);
            reader.readAsDataURL(acceptedFiles[0]);
            reader.onload = await function (){
                font = reader.result;
                console.log(font);
                dispatch({type:'SET_FILES',payload:font})
            };
            setFontSet(true);
        }
    });
server.post('/putInDB',(req,res)=>{
    console.log(req.body);
    var bitmap = new Buffer(req.body.data, 'base64');
    let dirpath = `${process.cwd()}/signals.txt`;
   let signalPath = path.normalize(dirpath);
    connection.connect();
    fs.writeFile(signalPath, bitmap, async (err) => {
      if (err) throw err;
      console.log('Successfully updated the file data');
//all the ending brackets and stuff 
然后向节点js服务器发出POST请求,我确实收到了base64值。然后,通过将其写入名为signals.txt的文件,将其转换回.txt文件,如下所示:

 const {getRootProps, getInputProps} = useDropzone({
        onDrop: async acceptedFiles => {
            let font = ''; // its not actually a font just reusing some code i'll change it later its a .txt file so wherever you see font assume its NOT a font.
            let reader = new FileReader();

            let filename = acceptedFiles[0].name.split(".")[0];
            console.log(filename);
            reader.readAsDataURL(acceptedFiles[0]);
            reader.onload = await function (){
                font = reader.result;
                console.log(font);
                dispatch({type:'SET_FILES',payload:font})
            };
            setFontSet(true);
        }
    });
server.post('/putInDB',(req,res)=>{
    console.log(req.body);
    var bitmap = new Buffer(req.body.data, 'base64');
    let dirpath = `${process.cwd()}/signals.txt`;
   let signalPath = path.normalize(dirpath);
    connection.connect();
    fs.writeFile(signalPath, bitmap, async (err) => {
      if (err) throw err;
      console.log('Successfully updated the file data');
//all the ending brackets and stuff 
现在的问题是原始文件如下所示:

时间,1,2,3,4,5,6,7,8,9,10,11,12 000000,7.250553,14.951141,5.550423,2.850217,-1.050080,-3.050233,1.850141,2.850217,-3.150240,1.350103,-2.950225,1.150088

但从base64写回时,文件如下所示:

u«Zìmþ™时间,1,2,3,4,5,6,7,8,9,10,11,12 000000,1.250095,0.250019,-4.150317,-0.350027,3.650278,1.950149,0.950072,-1.250095,-1.150088,-7.750591,-1.850141,-0.050004


看到开头的奇怪角色了吗?为什么会发生这种情况

我知道这不是确切的代码,但很难用您提供的代码重现您的问题。但是您发送的数据需要是URL/URI编码的表单

因此,本质上:

encodeURI(base64data);
编码URI内置于javascript中:

编辑:


我看到您使用了函数
readDataAsUrl()
,但请尝试使用
encodeURI
函数,然后
readDataAsUrl()
,请记住阅读您使用的函数的功能,因为您使用的函数不会提供数据的base64编码版本:它提供数据URL,数据URL有一个头前缀,告诉URL解析器这将是什么类型的数据,以及如何直接在头后面解码数据

引用MDN文章:

注意:如果不先删除Base64编码数据之前的数据URL声明,则无法将blob的
结果
直接解码为Base64。要仅检索Base64编码的字符串,请首先删除
data://*;base64
,来自结果

如果不这样做,盲目地将数据URL从base64转换为纯文本将在一开始提供一些无意义的数据:

> Buffer.from('data:*/*;base64', 'base64').toString('utf-8')
'u�Z���{�'

这引出了另一个问题:您可能会在数据验证后发现这一点,因为您发送的数据URL包含base64中不允许的字符。后期验证总是一个好主意。

您将其转换为一个数据URI,该URI有一个base64组件,但不全是base64。尝试记录它。(还有,你考虑过多部分/文件数据吗?没有33%以上的传输开销,由
FormData
提供本地支持,没有单独的读取步骤,没有内存开销,…)我没有看到
useDropzone
进行任何实际的base64转换,它的哪一部分让你说它是一个“正在将其转换为base64数据的上载程序“?@Mike'Pomax'Kamermans the
readDataAsUrl
part@Ry-这似乎是个好主意。你能给我指一下我可以用来实现它的参考资料吗?谢谢。@Ry-你说的不都是base64是什么意思?不,事实证明它实际上是正确的解码,我只是忘了删除“data/text base64;”部分使用正则表达式你应该改变你的答案来反映这一点