当前位置:网站首页>Upload and modify the use of avatars
Upload and modify the use of avatars
2022-06-25 11:03:00 【Stranger & love sorrow】
Handwriting input Methods
1 Write the uploaded input Button
<input type='file' accept="image/*" @change='change'> Upload local file
type type , Indicates that the file is uploaded input
accept Type of upload , Represents a picture
@change Trigger after the file is selected
2 Corresponding change event
change(e) {
// Handwritten input You need an object to convert the local image into the corresponding format to upload
let formData = new FormData();
// e.target.files Is an array of selected files
formData.append("file", e.target.files[0]);
// take formData This object is passed to the interface that modifies the avatar
updateImg(formData).then(res => {
if (res.code == 200) {
Toast(res.msg);
// res.data.path The URL returned is the avatar , It also depends on the returned data
this.userData.avatar = res.data.path;
// Some projects need to send the successful image to the website , Send and modify data interface , See the background processing for details
updateUser({ avatar: res.data.path });
}
});
},
Corresponding input Change the style of
DOM structure
<a href="javascript:;" class="file"> Select File
<input type="file" name="" id="">
</a>
css structure
.file {
position: relative;
display: inline-block;
background: #D0EEFF;
border: 1px solid #99D3F5;
border-radius: 4px;
padding: 4px 12px;
overflow: hidden;
color: #1E88C7;
text-decoration: none;
text-indent: 0;
line-height: 20px;
}
.file input {
position: absolute;
font-size: 100px;
right: 0;
top: 0;
opacity: 0;
}
.file:hover {
background: #AADFFD;
border-color: #78C3F3;
color: #004974;
text-decoration: none;
}
The modified style is shown in the following figure
Click to see more specific style changes
vant Of Uploader Use of components
1 Use Here is the basic usage
<van-uploader :after-read="afterRead" />
2 Upload files
methods: {
afterRead(file) {
// file It's the handwriting on it input in , after FromData The picture address after packaging , It can be uploaded directly to the server
updateImg(file).then(res => {
if (res.code == 200) {
Toast(res.msg);
// res.data.path The URL returned is the avatar , It also depends on the returned data
this.userData.avatar = res.data.path;
// Some projects need to send the successful image to the website , Send and modify data interface , It depends on whether it has been handled in the background
updateUser({ avatar: res.data.path });
}
},
},
matters needing attention :
1 Handwriting input You need to use a FormData Object to convert the format , and vant Of file It's already converted
2 This converted format , It's an object , Directly treat the whole object as data Parameters can be passed , The specific situation depends on the project interface
3 After the transmission , The background will return data , Among them data.path Is the URL link of the modified avatar
4 Some projects are not processed in the background , We need to put this avatar website again , Pass to the interface for modifying user information , The purpose of modifying the Avatar has been achieved
elementUi Upload your avatar
<el-upload
:action="uploadURL" // Address or interface for uploading pictures , Full web address required
// Trigger when you click the uploaded file in the file list , With a parameter , There are http Picture format for , You can preview pictures
:on-preview="handlePreview"
// Trigger... When deleting files , With a parameter , There are http Picture format for
:on-remove="handleRemove"
// Set the request header file when uploading , Because some pictures need to be uploaded token, You can set it up here
:headers="headerObj"
// Type of file list
list-type="picture"
// Triggered when the file is uploaded successfully , There will also be parameters , You can find pictures in the corresponding format
:on-success="handleSuccess"
>
Be careful :
In every event , Will come with a parameter , You can find pictures in two formats ,https The Internet Format and "tmp_uploads\468f50432ed8b67b89e2490dc9e7f34d.jpg" Local Format , Which format of pictures will be uploaded at that time , See the requirements of the interface document
If you want to preview the picture , The bullet box needs to be written by yourself , Can be in on-preview In the incident , Get the network format of the currently clicked picture
Because the image upload interface or website here is an independent website ,action attribute Value , Must be a complete web address
headers Attributes are used to pass token Of , The specific format depends on the project requirements
边栏推荐
- SystemVerilog(十三)-枚举数据类型
- Is it safe for Guosen Securities to open a securities account
- Complete steps for a complete Oracle uninstall
- Google Earth Engine(GEE)——evaluate实现一键批量下载研究区内的所有单张影像(上海市部分区域)
- 西门子PLCS7-200使用(一)---开发环境和组态软件入门
- Flask blog practice - archiving and labeling of sidebar articles
- On binary tree
- 学会自学【学会学习本身,比学什么都重要】
- Network protocol learning -- lldp protocol learning
- 指南针在上面开股票账户安全吗?
猜你喜欢
SystemVerilog(十三)-枚举数据类型
[200 opencv routines] 210 Are there so many holes in drawing a straight line?
中国信通院沈滢:字体开源协议——OFL V1.1介绍及合规要点分析
Advanced single chip microcomputer -- development of PCB (2)
Explanation and use of kotlin syntax for Android
Android之Kotlin语法详解与使用
Unreal Engine graphics and text notes: use VAT (vertex animation texture) to make Houdini end on Houdini special effect (ue4/ue5)
COSCon'22 讲师征集令
OpenCV学习(二)---树莓派上安装opencv
我的作文题目是——《我的区长父亲》
随机推荐
OpenCV学习(一)---环境搭建
手机炒股安全吗?
SystemVerilog(十三)-枚举数据类型
NETCORE performance troubleshooting
《天天数学》连载52:二月二十日
Simple use of SVN
The title of my composition is - "my district head father"
性能之内存篇
Tutorial on installing SSL certificates in Microsoft Exchange Server 2007
单片机开发---基于ESP32-CAM的人脸识别应用
16 enterprise architecture strategies
好好思考
keep-alive
Advanced single chip microcomputer -- development of PCB (2)
Handling of NPM I installation problems
1-7Vmware中的快照与克隆
Cdn+cos ultra detailed steps for drawing bed construction
视频会议一体机的技术实践和发展趋势
之前字符串反转的题目
中國信通院沈瀅:字體開源協議——OFL V1.1介紹及合規要點分析