释放创意,无限存储:揭秘免费图床项目的终极指南与源代码大公开!

释放创意,无限存储:揭秘免费图床项目的终极指南与源代码大公开!

magic
2024-09-30 / 0 评论 / 18 阅读 / 正在检测是否收录...

实现图床项目

新建一个vue3项目

vue create bmob-viewer

Xnip2024-09-23_10-44-50.jpg

安装 element-plus

yarn add element-plus

放一个按钮看下是否引入成功

    <el-card class="bucket-select-card">
      <div>
        <el-button type="primary">按钮</el-button>
        <el-button type="primary" disabled>按钮</el-button>
      </div>

    </el-card>

Xnip2024-09-23_14-18-44.jpg

做一个上传界面,我们使用el-upload 组件

https://s-test.belle.cn/zh-CN/component/upload.html 照片墙代码

<template>
  <el-upload
    v-model:file-list="fileList"
    action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
    list-type="picture-card"
    :on-preview="handlePictureCardPreview"
    :on-remove="handleRemove"
  >
    <el-icon><Plus /></el-icon>
  </el-upload>

  <el-dialog v-model="dialogVisible">
    <img w-full :src="dialogImageUrl" alt="Preview Image" />
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'

import type { UploadProps, UploadUserFile } from 'element-plus'

const fileList = ref<UploadUserFile[]>([
  {
    name: 'food.jpeg',
    url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
  },
  {
    name: 'plant-1.png',
    url: '/images/plant-1.png',
  }
])

const dialogImageUrl = ref('')
const dialogVisible = ref(false)

const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
}

const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url!
  dialogVisible.value = true
}
</script>

Xnip2024-09-23_15-38-51.jpg

这个列表数据是写死的,我们从服务器文件记录表取出记录,这里我们使用Bmob后端云

安装sdk

npm install hydrogen-js-sdk

main.ts 初始化sdk

import Bmob from 'hydrogen-js-sdk'

// 初始化
Bmob.initialize("e7cebb69e9ed76e9", "111111");

取出数据

let fileList = ref<UploadUserFile[]>([])

const query = Bmob.Query('pic_list');
query.find().then(res => {
  // 确保 res 是 UploadUserFile[] 类型
  if (Array.isArray(res)) {
    fileList.value = res;
  } else {
    console.error('查询结果类型不匹配');
  }
});

效果

Xnip2024-09-25_16-23-05.jpg

实现删除


const handleRemove: UploadProps['onRemove'] = (uploadFile: ImageInfo, uploadFiles: ImageInfo[]) => {
  console.log(uploadFile);

  const query = Bmob.Query('pic_list');
  const objectId = uploadFile.objectId;

  query.destroy(objectId).then(res => {

    // 传入string是单个文件删除,传入array是批量删除
    const del = Bmob.File("a", "jfdf");
    const val = [uploadFile.url]
    del.destroy(val).then(res => {
      console.log(res);
    }).catch(err => {
      console.log(err)
    })
  }).catch(err => {
    console.error('删除文件失败:', err);
  });
};

实现上传

const customUpload = async (options) => {
  const file = Bmob.File(options.file.name, options.file);
  const res = await file.save();
  console.log("File saved successfully:", res);
  // 把URL保存到Bmob pic_list表中
  const query = Bmob.Query('pic_list');
  const data = {
    name: options.file.name,
    url: res[0].url
  };
  query.save(data).then(res => {
    console.log(res)
  }).catch(err => {
    console.log(err)
  })
}

最终效果

Xnip2024-09-30_17-08-02.jpg

项目开源代码地址

https://github.com/magic007/Vue3-image-bed

部署效果地址

http://images-bed.cctvcloud.cn/

1

评论

博主关闭了当前页面的评论