简体   繁体   English

Text -> PNG -> ReadStream,都在前端搞定了?

[英]Text -> PNG -> ReadStream, all done on the front-end?

I'm not sure if this is even possible, but here's what I'm trying to do:我不确定这是否可能,但这是我想要做的:

  1. Let the user enter some text让用户输入一些文本
  2. Generate a PNG from that text从该文本生成 PNG
  3. Upload it to Pinata, which requires it to be in ReadStream format上传到 Pinata,这要求它是ReadStream格式
  4. Do all of this on the front-end在前端完成所有这些

I've managed to accomplish (1) and (2) using html2canvas .我已经设法使用html2canvas完成(1)和(2)。

The tricky part is (3).棘手的部分是(3)。 The reason it has to be in ReadStream format is because that's the format Pinata's SDK wants:它必须采用ReadStream格式的原因是因为这是Pinata 的 SDK想要的格式:

const fs = require('fs');
const readableStreamForFile = fs.createReadStream('./yourfile.png');
const options = {
    pinataMetadata: {
        name: MyCustomName,
        keyvalues: {
            customKey: 'customValue',
            customKey2: 'customValue2'
        }
    },
    pinataOptions: {
        cidVersion: 0
    }
};
pinata.pinFileToIPFS(readableStreamForFile, options).then((result) => {
    //handle results here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

I realize that this would be no problem to do on the backend with node, but I'd like to do it on the front-end.我意识到这在后端用 node 做没有问题,但我想在前端做。 Is that at all possible?这可能吗? Or am I crazy?还是我疯了?

I'm specifically using Vue if that matters.如果重要的话,我专门使用 Vue。

For anyone interested the solution ended up being using fetch+blob:对于任何感兴趣的人,解决方案最终使用了 fetch+blob:

    const generateImg = async () => {
      const canvas = await html2canvas(document.getElementById('hello'));
      const img = canvas.toDataURL('image/png');
      const res = await fetch(img);
      return res.blob();
    };

This blob can then be passed into a more manual version of their SDK:然后可以将此 blob 传递到其 SDK 的更手动版本中:

const uploadImg = (blob: Blob) => {
    const url = `https://api.pinata.cloud/pinning/pinFileToIPFS`;

    const data = new FormData();
    data.append('file', blob);

    const metadata = JSON.stringify({
      name: 'testname',
    });
    data.append('pinataMetadata', metadata);

    const pinataOptions = JSON.stringify({
      cidVersion: 0,
    });
    data.append('pinataOptions', pinataOptions);

    return axios
      .post(url, data, {
        maxBodyLength: 'Infinity' as any, // this is needed to prevent axios from erroring out with large files
        headers: {
          // @ts-ignore
          'Content-Type': `multipart/form-data; boundary=${data._boundary}`,
          pinata_api_key: apiKey,
          pinata_secret_api_key: apiSecret,
        },
      })
      .then((response) => {
        console.log(response);
      })
      .catch((error) => {
        console.log(error);
      });
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Postgres DB获取PNG文本并将图像发送到前端React - Getting text of PNG from Postgres DB and sending image to front-end React 使用 React 的文本滑块前端 - Text Sliders front-end using React 动态网站需要前端,还是都是前端动态生成的? - Does a dynamic website need a front-end, or are all front-end dynamically generated? NodeJS 和前端 - NodeJS and front-end Google Text-toSpeech - 在前端获取音频文件 - Google Text-toSpeech - Getting the Audio File in Front-end 如何从用户在前端提交的 PNG 读取单个像素透明度值? - How to read individual pixel transparency values from a user submitted PNG on the front-end? 如何使前端页面离线(只访问本地主机和相对路径资产(js,css,png...)) - How to make front-end page offline(only access localhost and relative path assets(js,css, png…)) 如果Keycloak身份验证是在Apache级别而不是前端应用程序上完成的,是否可以获取用户数据 - Is there a way to obtain user data if the Keycloak authentication is done on Apache level, not front-end application 前端集成测试 - Front-end integration testing 再现前端错误 - Reproducing a Front-end Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM