uniapp文件上傳指南

一、使用uni.uploadFile上傳文件

1、首先在頁面中添加文件選擇器(input type=”file” accept=”image/*”)的組件。

2、為文件選擇器添加change事件,處理用戶選擇的文件並調用uni.uploadFile()函數上傳文件。

3、完成文件上傳後,服務器返回的數據可以在uni.uploadFile()的回調函數中處理。

 <template>
   <view>
      <input type="file" accept="image/*" @change="uploadFile" />
   </view>
</template>

<script>
export default {
   methods: {
      uploadFile(event) {
         const file = event.target.files[0];
         uni.uploadFile({
            url: 'http://localhost:3000/upload',
            filePath: file.tempFilePath,
            name: 'file',
            success(res) {
               console.log(res.data);
            }
         });
      }
   }
}
</script>

二、設置請求頭和參數

1、通過uni.uploadFile()第三個參數的header屬性設置請求頭。

2、通過uni.uploadFile()第三個參數的formData屬性設置參數。

 <script>
export default {
   methods: {
      uploadFile(event) {
         const file = event.target.files[0];
         uni.uploadFile({
            url: 'http://localhost:3000/upload',
            filePath: file.tempFilePath,
            name: 'file',
            header: {
               'Authorization': 'Bearer ' + uni.getStorageSync('token')
            },
            formData: {
               'name': 'avatar'
            },
            success(res) {
               console.log(res.data);
            }
         });
      }
   }
}
</script>

三、限制文件類型和大小

1、通過accept參數限制文件類型。

2、通過fileSize參數限制文件大小。

 <template>
   <view>
      <input type="file" accept="image/png,image/jpeg" :fileSize="1024 * 1024" @change="uploadFile" />
   </view>
</template>

<script>
export default {
   methods: {
      uploadFile(event) {
         const file = event.target.files[0];
         uni.uploadFile({
            url: 'http://localhost:3000/upload',
            filePath: file.tempFilePath,
            name: 'file',
            header: {
               'Authorization': 'Bearer ' + uni.getStorageSync('token')
            },
            formData: {
               'name': 'avatar'
            },
            success(res) {
               console.log(res.data);
            }
         });
      }
   }
}
</script>

四、顯示上傳進度

1、通過uni.uploadFile()函數的progress屬性實現上傳進度顯示。

2、在頁面中添加進度條組件,並將進度值綁定到data中的progress變量。

 <template>
   <view>
      <input type="file" accept="image/png,image/jpeg" :fileSize="1024 * 1024" @change="uploadFile" />
      <progress :percent="progress" />
   </view>
</template>

<script>
export default {
   data() {
      return {
         progress: 0
      }
   },
   methods: {
      uploadFile(event) {
         const file = event.target.files[0];
         uni.uploadFile({
            url: 'http://localhost:3000/upload',
            filePath: file.tempFilePath,
            name: 'file',
            header: {
               'Authorization': 'Bearer ' + uni.getStorageSync('token')
            },
            formData: {
               'name': 'avatar'
            },
            success(res) {
               console.log(res.data);
            },
            progressCallback(res) {
               this.progress = res.progress;
            }
         });
      }
   }
}
</script>

五、使用uni.compressImage()壓縮圖片

1、上傳大量圖片可能會導致上傳速度變慢,同時也會消耗用戶手機的流量。

2、通過uni.compressImage()函數將圖片壓縮後再上傳,可以減少上傳時間和流量消耗。

 <script>
export default {
   methods: {
      uploadFile(event) {
         const file = event.target.files[0];
         uni.compressImage({
            src: file.tempFilePath,
            quality: 80,
            success(res) {
               uni.uploadFile({
                  url: 'http://localhost:3000/upload',
                  filePath: res.tempFilePath,
                  name: 'file',
                  header: {
                     'Authorization': 'Bearer ' + uni.getStorageSync('token')
                  },
                  formData: {
                     'name': 'avatar'
                  },
                  success(res) {
                     console.log(res.data);
                  },
                  progressCallback(res) {
                     console.log(res.progress);
                  }
               });
            }
         });
      }
   }
}
</script>

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/183450.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-24 16:29
下一篇 2024-11-24 16:29

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • 運維Python和GO應用實踐指南

    本文將從多個角度詳細闡述運維Python和GO的實際應用,包括監控、管理、自動化、部署、持續集成等方面。 一、監控 運維中的監控是保證系統穩定性的重要手段。Python和GO都有強…

    編程 2025-04-29
  • Python應用程序的全面指南

    Python是一種功能強大而簡單易學的編程語言,適用於多種應用場景。本篇文章將從多個方面介紹Python如何應用於開發應用程序。 一、Web應用程序 目前,基於Python的Web…

    編程 2025-04-29
  • Python wordcloud入門指南

    如何在Python中使用wordcloud庫生成文字雲? 一、安裝和導入wordcloud庫 在使用wordcloud前,需要保證庫已經安裝並導入: !pip install wo…

    編程 2025-04-29
  • Python字符轉列表指南

    Python是一個極為流行的腳本語言,在數據處理、數據分析、人工智能等領域廣泛應用。在很多場景下需要將字符串轉換為列表,以便於操作和處理,本篇文章將從多個方面對Python字符轉列…

    編程 2025-04-29
  • Python小波分解入門指南

    本文將介紹Python小波分解的概念、基本原理和實現方法,幫助初學者掌握相關技能。 一、小波變換概述 小波分解是一種廣泛應用於數字信號處理和圖像處理的方法,可以將信號分解成多個具有…

    編程 2025-04-29
  • Python初學者指南:第一個Python程序安裝步驟

    在本篇指南中,我們將通過以下方式來詳細講解第一個Python程序安裝步驟: Python的安裝和環境配置 在命令行中編寫和運行第一個Python程序 使用IDE編寫和運行第一個Py…

    編程 2025-04-29
  • Python起筆落筆全能開發指南

    Python起筆落筆是指在編寫Python代碼時的編寫習慣。一個好的起筆落筆習慣可以提高代碼的可讀性、可維護性和可擴展性,本文將從多個方面進行詳細闡述。 一、變量命名 變量命名是起…

    編程 2025-04-29
  • FusionMaps應用指南

    FusionMaps是一款基於JavaScript和Flash的交互式地圖可視化工具。它提供了一種簡單易用的方式,將複雜的數據可視化為地圖。本文將從基礎的配置開始講解,到如何定製和…

    編程 2025-04-29
  • Python中文版下載官網的完整指南

    Python是一種廣泛使用的編程語言,具有簡潔、易讀易寫等特點。Python中文版下載官網是Python學習和使用過程中的重要資源,本文將從多個方面對Python中文版下載官網進行…

    編程 2025-04-29

發表回復

登錄後才能評論