爱深求 發表於 2022-6-14 17:04:00

uni-app上传图片和文件

<p>如图所示:</p>
<p>&nbsp;<img src="https://img2022.cnblogs.com/blog/1161361/202206/1161361-20220614170157977-702025124.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>上传图片,使用的是uni.chooseImage这个官方api,count 数量根据自己的需求来,我们是最多只能上传9张</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">          uploadImgEvent(){
             uni.chooseImage({
                  count: </span>10 - <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.uploadImgsList.length,
                  success: (res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                  </span><span style="color: rgba(0, 0, 255, 1)">this</span>.uploadImgsList.unshift(...res.tempFiles.map(item =&gt;<span style="color: rgba(0, 0, 0, 1)"> ({
                        picturePath: item.path
                  })));
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">this</span>.uploadImgsList.length == 10) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.uploadImgsList.pop();
                  }
            });
            },</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">uploads(){
    const _this </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">return</span>Promise.all(_this.uploadImgsList.map(item=&gt;_this.uploadImage(item.picturePath))).then(res=&gt;<span style="color: rgba(0, 0, 0, 1)">{
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> res.map(item=&gt;<span style="color: rgba(0, 0, 0, 1)">({
               fileName:item.fileName,
               filePath:item.url
            }))
   })
},
uploadImage(url) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Promise(async (resolve, reject) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      uni.uploadFile({
            url: await getUploadUrl(), </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">后台地址</span>
<span style="color: rgba(0, 0, 0, 1)">            filePath: url,
            name: </span>'file'<span style="color: rgba(0, 0, 0, 1)">,
            success: (uploadFileRes) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                resolve(JSON.parse(uploadFileRes.data));
            }
      })
    })
},</span></pre>
</div>
<p>&nbsp;</p>
<p><br><br>提交给后代的数据</p>
<p>         //提交<br>                   async submitFlood(){<br>                       let photoList = await this.uploads();<br>                        }<br> 打印photoList如图:</p>
<p>&nbsp;<img src="https://img2022.cnblogs.com/blog/1161361/202206/1161361-20220614170244278-970541362.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p> 上传文件使用的LFile 这个插件 https://ext.dcloud.net.cn/plugin?id=4109</p>
<p>根据官网案例来</p>
<div class="cnblogs_code">
<pre>
         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">上传附件</span>
<span style="color: rgba(0, 0, 0, 1)">            uploadFile() {
                const that </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 0, 255, 1)">if</span>(that.imgUploadList.length &gt;= 9<span style="color: rgba(0, 0, 0, 1)">){
                  </span><span style="color: rgba(0, 0, 255, 1)">this</span>.$mHelper.toast('最多上传9张'<span style="color: rgba(0, 0, 0, 1)">)
                  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
                }
                that.$refs.lFile.upload({
                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #ifdef APP-PLUS</span>
<span style="color: rgba(0, 0, 0, 1)">                        currentWebview: that.$mp.page.$getAppWebview(),
                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #endif</span>
                        url: 'xxxxxx', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">你的上传附件接口地址</span>
                        name: 'file'<span style="color: rgba(0, 0, 0, 1)">
                        },
                  });
            },</span></pre>
</div>
<div class="cnblogs_code">
<pre>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">上传成功</span>
<span style="color: rgba(0, 0, 0, 1)">            upSuccess(res) {
                let url </span>=<span style="color: rgba(0, 0, 0, 1)"> res.root.url;
                let name </span>=<span style="color: rgba(0, 0, 0, 1)"> res.root.originalName;
                let fileType </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.isFileType(res.root.type);
                let size </span>=<span style="color: rgba(0, 0, 0, 1)"> res.root.size;
                </span><span style="color: rgba(0, 0, 255, 1)">if</span>(fileType == 'image'<span style="color: rgba(0, 0, 0, 1)">){
                  </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.imgUploadList.push({url,name,fileType,size});
                }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
                  </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.fileUploadList.push({url,name,fileType,size})
                }
            },
         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据文件后缀名来判断,展示对应的图标</span>
<span style="color: rgba(0, 0, 0, 1)">      isImage(type){
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> ['png','jpg','jpeg','gif','svg'<span style="color: rgba(0, 0, 0, 1)">].includes(type.toLowerCase());
            },
            isDocx(type){
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> ['doc','docx'<span style="color: rgba(0, 0, 0, 1)">].includes(type.toLowerCase());
            },
            isXsls(type){
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> ['xsls','xsl'<span style="color: rgba(0, 0, 0, 1)">].includes(type.toLowerCase());
            },
            isText(type){
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> ['text','txt'<span style="color: rgba(0, 0, 0, 1)">].includes(type.toLowerCase());
            },
            isFileType(type){
                </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">this</span>.isImage(type)) <span style="color: rgba(0, 0, 255, 1)">return</span> 'image'<span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">this</span>.isXsls(type)) <span style="color: rgba(0, 0, 255, 1)">return</span> 'excel'<span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 0, 255, 1)">if</span>(type == 'pdf') <span style="color: rgba(0, 0, 255, 1)">return</span> 'pdf'<span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">this</span>.isDocx(type)) <span style="color: rgba(0, 0, 255, 1)">return</span> 'word'<span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">this</span>.isText(type)) <span style="color: rgba(0, 0, 255, 1)">return</span> "text"<span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> return "#icon-file-b--6";</span>
<span style="color: rgba(0, 0, 0, 1)">            },
         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">文件预览</span>
<span style="color: rgba(0, 0, 0, 1)">            previewFile(item){
                uni.downloadFile({
                  url: item.url,
                  success: (res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                        let filePath </span>=<span style="color: rgba(0, 0, 0, 1)"> res.tempFilePath;
                        uni.openDocument({
                            filePath: filePath,
                            success: (res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                              console.log(</span>'打开文档成功'<span style="color: rgba(0, 0, 0, 1)">);
                            }
                        })
                  }
                })
            },</span></pre>
</div>
<p>&nbsp;</p>
<p><br><br></p>

</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:喆星高照,转载请注明原文链接:https://www.cnblogs.com/houxianzhou/p/16375411.html</p><br><br>
来源:https://www.cnblogs.com/houxianzhou/p/16375411.html
頁: [1]
查看完整版本: uni-app上传图片和文件