Home  >  Article  >  Web Front-end  >  How to use uniapp to develop avatar upload function

How to use uniapp to develop avatar upload function

WBOY
WBOYOriginal
2023-07-04 09:45:162820browse

How to use uniapp to develop avatar upload function

Introduction:
In modern social networks and applications, avatars have become a very important element. Users need to be able to upload and change their avatar easily. This article will introduce how to use uniapp to develop a simple avatar upload function, and provide code examples for your reference.

1. Preparation work
Before starting development, we need to install the corresponding development environment and tools. First, make sure node.js and npm are installed. Then, we need to install uniapp’s scaffolding tool HBuilderX. Run the following command in the command line to complete the installation:

npm install -g @vue/cli

2. Create a uniapp project
Use HBuilderX to create a new uniapp project. Open HBuilderX, select "File"->"New"->"uniapp project". Enter a project name, select the appropriate template, and click the "Create" button.

3. Add the avatar upload component
In the root directory of the uniapp project, find the pages folder and create a new page in it, named "avatarUpload". In the vue file of the page, add the following code:

<template>
  <view class="container">
    <image class="avatar" :src="avatar" mode="aspectFill"></image>
    <button class="btn" @click="selectImage">选择头像</button>
    <button class="btn" @click="uploadImage">上传头像</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      avatar: '' // 用于存储头像的base64编码
    }
  },
  methods: {
    selectImage() {
      uni.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          this.avatar = res.tempFilePaths[0]
        }
      })
    },
    uploadImage() {
      uni.uploadFile({
        url: 'http://your-upload-api-url',
        filePath: this.avatar,
        name: 'file',
        header: {
          'Authorization': 'Bearer ' + uni.getStorageSync('token')
        },
        success: (res) => {
          uni.showToast({
            title: '上传成功'
          })
        }
      })
    }
  }
}
</script>

<style>
.container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  height: 100vh;
}

.avatar {
  width: 200px;
  height: 200px;
  margin-bottom: 20px;
}

.btn {
  width: 150px;
  height: 40px;
  margin-bottom: 20px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
}
</style>

In the above code, we created a simple page that contains a picture element to display the selected avatar and two buttons for selection Avatar and upload avatar. In the selectImage method, we use the uni.chooseImage method to select the avatar and save its path to the avatar variable in data. In the uploadImage method, we use the uni.uploadFile method to upload the avatar to the server, and display a successful upload message when successful.

4. Use the avatar upload component in other pages
If you want to use the avatar upload component in other pages, just add the following code to the corresponding vue file:

<template>
  <view>
    <avatar-upload></avatar-upload>
  </view>
</template>

<script>
import avatarUpload from '@/pages/avatarUpload'

export default {
  components: {
    avatarUpload
  }
}
</script>

Above The code introduces the avatar upload component into the current page as a subcomponent. You can place it in the appropriate location as needed.

Summary:
This article introduces how to use uniapp to develop a simple avatar upload function. We created an avatar upload component and provided code examples. You can modify and extend it according to your needs. Through the guidance of this article, you can quickly implement a convenient avatar upload function.

However, it should be noted that the upload method in the above example is a simplified version. In actual projects, you need to make corresponding modifications and adjustments based on your own back-end API. I hope this article can be helpful to you, and I wish you smooth development!

The above is the detailed content of How to use uniapp to develop avatar upload function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn