简体   繁体   English

如何将文件输入内容存储在变量中供以后在 Nodejs 中使用?

[英]How to store file input content in a variable for later use in Nodejs?

I'm trying to create a Dapp that let you upload an image to IPFS with Nodejs.我正在尝试创建一个 Dapp,让您可以使用 Nodejs 将图像上传到 IPFS。 I created the next form:我创建了下一个表格:

<form id="upload_form">
    <input id="upload_image" type="file"></input>
    <input type="submit"/>
</form>

And js code is like this:而js代码是这样的:

import Web3 from "web3";
import amongAllArtifact from "../../build/contracts/AmongAll.json";

//const ipfs = require("ipfs-http-client");

const App = {
  web3: null,
  account: null,
  contract: null,

  file: null,


  start: async function() {
    const { web3 } = this;

    try {
      // get contract instance
      const networkId = await web3.eth.net.getId();
      const deployedNetwork = contractArtifact.networks[networkId];
      this.contract = new web3.eth.Contract(
        contractArtifact.abi,
        deployedNetwork.address,
      );

      // get accounts
      const accounts = await web3.eth.getAccounts();
      this.account = accounts[0];

      this.refreshAddress();

    } catch (error) {
      console.error("Could not connect to contract or chain.");
    }
  },

};

window.App = App;

window.addEventListener("load", function() {
  
  App.start();
});

What I wanted to do is to execute a function when the file input changes so that I could upload it to some IPFS node.我想做的是在文件输入更改时执行 function,以便我可以将其上传到某个 IPFS 节点。 The thing is that I really dont know how to do it.问题是我真的不知道该怎么做。 I've tried this:我试过这个:

setFile: async function(file){

    this.file(file);

  },


  capture_change: document.getElementById('upload_image').addEventListener('change', function(event) {
    
    event.preventDefault();
    console.log("procesando imgane...");
    
    var f = event.target.files[0];

    console.log("files: ", f)

    const reader = new window.FileReader();
    reader.readAsArrayBuffer(f)
    
    reader.onloadend = () => {
      
      setFile(Buffer(reader.result));
      console.log(this.file);
    }
  }),

The second function trigger when the input changes and what I wantted was to store the content in some variable so that I could get it when the submit event was triggered.第二个 function 在输入更改时触发,我想要的是将内容存储在某个变量中,以便在触发提交事件时可以获取它。 For that I created the first function setFile, because from inside reader.onloadend() it doesnt detect any "file" attribute, but it doesnt detect setFile either.为此,我创建了第一个 function setFile,因为从 reader.onloadend() 内部它没有检测到任何“文件”属性,但它也没有检测到 setFile。

How can I store the "f" content in somewhere that when a submit event is trigger I could get it back??我如何将“f”内容存储在某个地方,以便在触发提交事件时我可以将其取回?

Try converting it to base64 and storing the base64 string;尝试将其转换为 base64 并存储 base64 字符串; you can encode it back whenever you need.您可以在需要时将其编码回来。

var fs = require('fs');
var base64img = fs.readFileSync(file, 'base64');

I'm thinking if you want to do it client side, aside from placing it in a canvas the best option I believe is convert it to B64 and use it as you need.我在想,如果你想在客户端进行,除了将它放在 canvas 中之外,我认为最好的选择是将它转换为 B64 并根据需要使用它。

暂无
暂无

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

相关问题 如何存储变量的内容供以后在其他函数中使用 (Javascript) - How do I store the content of a variable for later use in other function (Javascript) 如何从 Nodejs 访问 aws 参数存储值并在以后使用它 - how to access aws parameter store value from Nodejs and use it later 如何将 mongodb findone 值保存到 javascript 变量以便稍后在 nodejs 上使用 - How to save mongodb findone value to a javascript variable to use it later on nodejs 如何将数组保存到文件中,然后再将其读入nodejs / JavaScript中的数组变量中? - How to save an array to file and later read it into an array variable in nodejs/JavaScript? 您如何将 getText 的结果存储在变量中以供以后在 Nightwatch 中使用? - How do you store the result of getText in a variable to use later on in Nightwatch? 如何将while循环中生成的值存储到变量中以供以后在JQuery中使用 - How to store a value generated in a while loop to a variable for later use in JQuery 如何将表单中的值存储在变量中以供以后在函数中使用? - How to store the value from a form in a variable for later use in a function? 如何将用户输入存储为 JavaScript 变量以备后用 - How to make user input stored as JavaScript Variable for later use 如何将画布存储到变量中,然后在稍后渲染它? - How to store a canvas into a variable, and then render it at later point? 如何存储参考 DOM 元素以备后用 - How to store a reference DOM element for later use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM