网站首页 > 博客文章 正文
转载说明:原创不易,未经授权,谢绝任何形式的转载
在开发前端应用程序时,有时我们可能需要允许用户上传图像文件。稍后,我们可以处理图像文件,并在必要时将其发送到后端进行进一步处理和存储。最常见的图像文件格式是PNG、JPEG和JPG。
当用户上传图片时,我们必须首先将文件内容嵌入为base64或文件,并将信息附加到formData中,然后再将其与请求体一起发送。
将图像文件嵌入formData时,必须将其作为键值对包含在内。键应表示包含图像文件的主体数据的名称,相应的值应包含实际的图像文件。我们还需要确保设置正确的头部编码类型为 multipart/form-data 。
multipart/form-data 是一种HTML表单编码类型,用于当我们的表单包含任何 <input type="file"> 元素时使用。当一个表单包含文件输入时,必须在HTML表单标签的enctype属性中使用"multipart/form-data",以确保在服务器端进行正确的数据传输和处理。
formData提供了一个我们可以使用各种操作来操作的对象。其中一些最常见的操作包括:
- formData.append():用于向指定对象键添加新值。
- formData.get(): 用于检索与特定键关联的第一个值。
- formData.delete(): 用于从formData对象中删除键/值对。
图片上传
<script setup lang="ts">
import { ref } from 'vue'
import axios from 'axios';
const selectedFile = ref();
async function onFileSelected(event: Event) {
if (!event.target)
return
const target = event.target as HTMLInputElement
if (!target.files) {
// toastError('No image selected')
return
}
console.log(target.files)
selectedFile.value = target.files
}
async function submitImageForm(){
const config = {
headers: {
"Content-Type": "multipart/form-data",
},
};
try{
// Your backend endpoint for image upload
const uploads_url = 'https://api.your-backend-url.com/upload';
let formData = new FormData();
formData.append("photo", selectedFile.value);
const res = await axios.post(`${uploads_url}`, formData, config);
const data = await res.json();
console.log(data)
}catch(error: unknown){
// Catch errors here and handle them accordingly
if (axios.isAxiosError(error) && error.response) {
console.log(error.response.data.message)
return
}
// handle other errors here
console.log(error)
}
}
</script>
<template>
<h2>Upload Image</h2>
<form method="post" enctype="multipart/form-data">
<label for="image">Select an image to upload:</label>
<input type="file" id="image" name="image" accept="image/*" @change="onFileSelected">
<br>
<input type="submit" value="Upload Image" @click="submitImageForm">
</form>
</template>
有一件重要的事情需要注意,那就是确保您使用与后端期望的formData一致的正确键名。这是必要的,因为它作为上传图像数据的标识符,并且后端将使用它来访问请求负载中的图像。
当您需要上传多个图片时,您可以像这样将 multiple 属性传递给输入字段。
<input
id="image"
type="file"
name="image"
accept="image/*"
@change="onFileSelected"
multiple
/>
当您将上传的图像内容记录到控制台时,您将收到类似于下面截图示例中显示的元数据。
结论
我们已经看到了如何上传图像文件,提取图像数据,将其附加到formData中,并使用适当的头部编码类型将其发送到后端。
由于文章内容篇幅有限,今天的内容就分享到这里,文章结尾,我想提醒您,文章的创作不易,如果您喜欢我的分享,请别忘了点赞和转发,让更多有需要的人看到。同时,如果您想获取更多前端技术的知识,欢迎关注我,您的支持将是我分享最大的动力。我会持续输出更多内容,敬请期待。
猜你喜欢
- 2024-09-17 一文带你搞懂前端登陆设计(前端登录页面模板)
- 2024-09-17 WANGEDITOR 实现CTRL+V粘贴图片并上传、WORD粘贴带图片
- 2024-09-17 前端的鸿蒙开发:照片上传之选择照片mate30 4.2
- 2024-09-17 黑客入门实践:如何绕过前端过滤上传文件
- 2024-09-17 ueditor粘贴word图片且图片文件自动上传功能
- 2024-09-17 前端上传前预览文件 image、text、json、video、audio「实践」
- 2024-09-17 WANGEDITOR如何能实现直接粘贴把图片上传到服务器中?
- 2024-09-17 前端上传文件或者上传文件夹(前端实现文件上传功能)
- 2024-09-17 前端上传图片被旋转的处理方案(万国觉醒金武将)
- 2024-09-17 vue实战024:Vue-Quill-Editor自定义图片上传详解
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)