惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Troy Hunt's Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Schneier on Security
Schneier on Security
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Help Net Security
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Palo Alto Networks Blog
P
Privacy International News Feed
AWS News Blog
AWS News Blog
Forbes - Security
Forbes - Security
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
A
Arctic Wolf
Hacker News: Ask HN
Hacker News: Ask HN
P
Proofpoint News Feed
N
News and Events Feed by Topic
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
Google Online Security Blog
Google Online Security Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
PCI Perspectives
PCI Perspectives
Know Your Adversary
Know Your Adversary
The Hacker News
The Hacker News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tenable Blog
S
Security Affairs
P
Privacy & Cybersecurity Law Blog
W
WeLiveSecurity
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
S
Securelist
AI
AI
Latest news
Latest news
T
The Blog of Author Tim Ferriss
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CXSECURITY Database RSS Feed - CXSecurity.com
美团技术团队
G
GRAHAM CLULEY
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
V
Visual Studio Blog
L
LangChain Blog

博客园 - 小虫1

AI 视频修复技术落地对比:四款去水印工具的技术路线解析 浏览器端本地视频处理实测:4 种去水印方案的技术原理与效果对比 前端开发者视角:一个视频去水印工具的技术猜想 记一次本地视频去水印踩坑经历——从本地部署到在线工具 前端实现视频去水印的几种技术方案对比 豆包视频去水印工具横向测评:麻雀 AI 工具箱与 3 款海外工具实测对比 视频去水印工具实战横评:少谈“黑科技”,多聊实际能用的 2026 在线视频去水印工具实测:4 种方案对比,本地处理首选麻雀AI工具箱 豆包生成的图片,怎么去水印? 2026年,GEO到底怎么做?-麻雀GEO Topaz Video AI的替代品 -麻雀AI视频修复工具,免费、安全、高效的Topaz替代选择 视频清晰度增强网站-麻雀AI视频修复工具 vue2 函数式调用一个弹框组件 跟框架无关的前端换皮肤 前端请求跨域,解决方案 ssh2-sftp-client 上传文件至服务器 beego 注册路由失效404,commentsRouter.go没有生成 前端代码实现整体缩放网页,做到缩放分辨率的效果 前端 docker jenkins mkdocs生成的网站为何很慢 重新搞nginx vue公共布局组件Layout引入 重新认识布局:你真的懂flex吗 重新认识布局:html和body元素 重新认识布局:3d空间中的css盒子 重新认识布局:百分比单位 重新认识布局:标准流,浮动,定位的关系 微信小程序 等宽不等高瀑布流
vue项目下自己封装上传文件功能
小虫1 · 2022-04-14 · via 博客园 - 小虫1

axios因为统一封装过,通过这个库上传文件也行,但是封装的地方修改不太容易,遂另起炉灶。

看代码:

// utils/uploadFile.js
const baseUrlConfig = {
	'development': 'http://localhost:8089/v1',
	'production': 'http://47.96.112.186/v1'

}
const env = process.env.NODE_ENV
//单个上传文件,接受一个获取到的文件参数
function upload(path, file) {
	return new Promise((resolve, reject) => {
		if (!file) {
			return alert("你还未选择文件");
		}
		var formData = new FormData();
		// 接口只接受一个文件,取第一个文件即可
		formData.append("image", file);
		// 我们的接口需要这2个参数
		formData.append("siteId", 1);
		formData.append("session", localStorage.getItem("sessionId"));
		// 1. 创建 xhr 对象
		var xhr = new XMLHttpRequest();
		// 5. 监听事件
		xhr.onreadystatechange = function () {
			console.log(234);
			if (xhr.readyState === 4 && xhr.status === 200) {
				var result = JSON.parse(xhr.responseText);
				console.log(result);
				if (result.status == 0) {
					// 上传成功:显示图片
					resolve(result.data)
				} else {
					// 上传失败
					reject(result.reason)
				}
			}
		};

		// 2. 调用 open 函数
		xhr.open("POST", baseUrlConfig[env] + path);
		// 4. 调用 send 函数
		xhr.send(formData);
	})
}

export default upload;
<template>
  <a-card>
    <a-space class="operator">每天上传的图片不要超过5张</a-space>
    <div class="box">
      <!-- 1. 文件选择框 -->
      <input type="file" id="file" />
      <!-- 2. 上传文件的按钮 -->
      <button id="btnUpload" @click="btnUpload">上传文件</button>
      <div class="img-box" v-if="imgUrl">
        <p>图片地址:{{ imgUrl }}</p>
        <!-- 3. img 标签,来显示上传成功以后的图片 -->
        <img :src="imgUrl" alt="等待上传图片" id="img" width="800" />
      </div>
    </div>
  </a-card>
</template>

<script>
import dateFormat from "@/utils/dateFormat.js";
import upload from "@/utils/uploadFile.js";
export default {
  data() {
    return {
      imgUrl: "",
    };
  },
  filters: {
    dateFormat(value) {
      return dateFormat(value, "yyyy-MM-dd hh:mm:ss");
    },
  },
  created() {},
  activated() {},
  mounted() {},
  methods: {
    btnUpload() {
      let file = document.querySelector("#file").files[0];
      upload("/admin/uploadFile", file).then((res) => {
        this.imgUrl = res;
      });
    },
  },
};
</script>

<style lang="less" scoped>
.box {
  height: 500px;
  padding: 20px 0 0;
  .img-box {
    margin-top: 30px;
    text-align: center;
    img {
      width: auto;
      height: 400px;
    }
  }
}
</style>