Vue2 结合 IPFS (星际文件系统)实现文件上传

1、预先安装配置好IPFS

2、运行 ipfs help (如出现下图安装ipfs完成)

 3、运行 ipfs daemon (出现下图表示成功)

 

4、利用vue创建一个项目

 vue create ipfs

 

5、出现下列文件

6、在如下目录中 编写  IPFS.vue

<template>
    <div class="home">
        <el-container>
            <el-form label-width="80px">
                <el-form-item label="产品图片">
                    <div class="img-wrapper">
                        <img class="img-panel" :src="photoURL1" />
                        <div class="txt-title">{{ipfsHash}}</div>
                    </div>
                    <el-upload class="img-upload" action="tmp" :limit="1" :on-change="handleChange"
                        :on-exceed="handleExceed" :auto-upload="false">
                        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                        <el-button style="margin-left: 10px;" size="small" type="success" @click="uploadImage">
                            上传到服务器
                        </el-button>
                    </el-upload>
                    <el-input v-model="photoURL1"></el-input>
                </el-form-item>
            </el-form>
        </el-container>
    </div>
</template>
  
<script>
import { create } from 'ipfs-http-client'
import Buffer from "vue-buffer";

export default {
    data() {
        const ipfs = create('http://192.168.1.161:5001/')
        return {
            photoURL1: '',
            fileList: [],
            ipfs: ipfs,
            buffer: null,
            ipfsHash: ''
        };
    },
    mounted() {

    },
    methods: {
        handleChange(file, fileList) {
            this.fileList = fileList;
        },
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
        },
        uploadImage() {
            if (this.fileList.length < 1) {
                this.$message.warning('请选择需要上传的文件')
            } else {
                const reader = new window.FileReader()
                console.log(this.fileList[0].raw)
                reader.readAsArrayBuffer(this.fileList[0].raw)
                reader.onloadend = () => {
                    this.buffer = Buffer(reader.result)
                    this.uploadToIPFS()
                }
            }
        },
        async uploadToIPFS() {
            let result = await this.ipfs.add(this.buffer)
            console.log(result)
            this.photoURL1 = `https://ipfs.io/ipfs/${result.path}`
            this.ipfsHash = `${result.path}`;
        },
    },
};
</script>

<style>
body>.el-container {
    margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
    line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
    line-height: 320px;
}
</style>

7、修改 App.vue 代码如下

<template>
  <div id="app">
    <IPFS/>
  </div>
</template>

<script>
import IPFS from './components/IPFS.vue'

export default {
  name: 'App',
  components: {
    IPFS
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

8、修改 main.js 如下

import Vue from 'vue'
import App from './App.vue'
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";

Vue.use(ElementUI);

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')
~                         

9、由于用到了element ui,所以需要下载依赖

npm install element-ui --save

10、下载依赖

npm install --save vue-buffer

11、在node_modules所在目录可以启动程序

npm run serve

页面如下:

 

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想买CT5的小曹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值