Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

uniapp 选择图片并转为base64

  1. User

    uniapp 选择图片并转为base64

  2. Assistant

    以下是使用uniapp选择图片并转为base64的示例代码:

    html代码:

    <template>
      <view class="content">
        <button @tap="chooseImage">选择图片</button>
        <img v-if="imageData" :src="imageData" />
      </view>
    </template>
    

    script代码:

    <script>
    export default {
      data() {
        return {
          imageData: "" // 存储base64图片数据
        };
      },
      methods: {
        chooseImage() {
          uni.chooseImage({
            count: 1, // 只选择一张图片
            success: res => {
              const tempFilePaths = res.tempFilePaths;
              uni.getFileSystemManager().readFile({
                filePath: tempFilePaths[0],
                encoding: "base64",
                success: res => {
                  this.imageData = "data:image/png;base64," + res.data; // 存储base64图片数据
                }
              });
            }
          });
        }
      }
    };
    </script>
    

    以上代码实现了选择一张图片并将其转为base64数据,并在页面上显示。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.