直饮水 發表於 2021-2-7 18:36:00

uni-app 图片上传

<h2><strong>uni.uploadFile(OBJECT)</strong></h2>
<p>将本地资源上传到开发者服务器,客户端发起一个POST请求,其中content-type为multipart/form-data.</p>
<p>如页面通过uni.chooseImage等接口获得一个本地资源的临时危机路径后,可通过此接口将本地资源上传到指定服务器。</p>
<p><strong>OBJECT 参数说明:</strong></p>
<table style="height: 438px; width: 824px" border="0">
<tbody>
<tr>
<td style="text-align: center"><strong>参数名</strong></td>
<td style="text-align: center"><strong>类型</strong></td>
<td style="text-align: center"><strong>必填</strong></td>
<td style="text-align: center"><strong>说明</strong></td>
<td style="text-align: center"><strong>平台支持</strong></td>
</tr>
<tr>
<td>url</td>
<td>String</td>
<td>是</td>
<td>开发者服务器url</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>files</td>
<td>Aarry</td>
<td>否</td>
<td>需要上传的文件列表。使用files时,filePath和name不生效。</td>
<td>5+app</td>
</tr>
<tr>
<td>filePath</td>
<td>String</td>
<td>是</td>
<td>要上传文件资源的路径</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>name</td>
<td>String</td>
<td>是</td>
<td>文件对应得key,开发者在服务器端通过这个key可以获取到文件二进制内容。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>header</td>
<td>Object</td>
<td>否</td>
<td>HTTP请求 Header,header中不能设置Referer。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>formData</td>
<td>Object</td>
<td>否</td>
<td>HTTP请求中其他额外的form data</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>success</td>
<td>Function</td>
<td>否</td>
<td>接口调用成功的回调函数</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>fail</td>
<td>Function</td>
<td>否</td>
<td>接口调用失败的回调函数</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>complete</td>
<td>Function</td>
<td>否</td>
<td>接口回调结束的回调函数(调用成功、失败都会执行)</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>图片上传及进度条显示</p>
<div class="cnblogs_code">
<pre>&lt;template&gt;
    &lt;View&gt;<span style="color: rgba(0, 0, 0, 1)">
      {{name}}</span>---<span style="color: rgba(0, 0, 0, 1)">{{age}}
      </span>&lt;!-- 上传进度条 --&gt;
      &lt;progress :percent="percent" stroke-width="10"&gt;&lt;/progress&gt;
      &lt;button @click="img"&gt;选择照片&lt;/button&gt;
    &lt;/View&gt;
&lt;/template&gt;

&lt;script&gt;
<span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> _self;
    export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
      data(){
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">{
                percent:</span>0<span style="color: rgba(0, 0, 0, 1)">,
                age:</span>''<span style="color: rgba(0, 0, 0, 1)">,
                name:</span>''<span style="color: rgba(0, 0, 0, 1)">
            }
      },
      onLoad: </span><span style="color: rgba(0, 0, 255, 1)">function</span> (option) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">option为object类型,会序列化上个页面传递的参数</span>
            console.log(option.age); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打印出上个页面传递的参数。</span>
            console.log(option.name); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打印出上个页面传递的参数。</span>
            <span style="color: rgba(0, 0, 255, 1)">this</span>.age=<span style="color: rgba(0, 0, 0, 1)">option.age;
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name=<span style="color: rgba(0, 0, 0, 1)">option.name;
            _self</span>=<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
      },
      methods:{
            img:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
                uni.chooseImage({
                  count: </span>1<span style="color: rgba(0, 0, 0, 1)">,
                  sizeType:</span>"compressed"<span style="color: rgba(0, 0, 0, 1)">,
                  success: (res) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                        console.log(res);
                        </span><span style="color: rgba(0, 128, 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, 128, 0, 1)"> uni.previewImage({</span>
                        <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   urls: res.tempFilePaths</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, 255, 1)">var</span> imgsFile=res.tempFilePaths;
                        </span><span style="color: rgba(0, 0, 255, 1)">var</span> uper=<span style="color: rgba(0, 0, 0, 1)">uni.uploadFile({
                            url: </span>'https://demo.hcoder.net/index.php?c=uperTest'<span style="color: rgba(0, 0, 0, 1)">,
                            filePath: imgsFile,
                            name: </span>'file'<span style="color: rgba(0, 0, 0, 1)">,
                            success:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(res1){
                              console.log(res1);
                            }
                        });
                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">修改进度条</span>
                        uper.onProgressUpdate(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(e){
                            _self.percent</span>=<span style="color: rgba(0, 0, 0, 1)">e.progress;
                            console.log(</span>'上传进度'+<span style="color: rgba(0, 0, 0, 1)">e.progress);
                            console.log(</span>'已上传数据长度'+<span style="color: rgba(0, 0, 0, 1)">e.totalBytesSent);
                            console.log(</span>'数据总长度'+<span style="color: rgba(0, 0, 0, 1)">e.totalBytesExpectedToSend);
                        });
                  }
                })
            }
      }
    }
</span>&lt;/script&gt;

&lt;style&gt;
   
&lt;/style&gt;</pre>
</div>
<h2>uni.downloadFile(OBJECT)</h2>
<p>下载文件资源到本地,客户端直接发起一个HTTP GET请求,返回文件的本地临时路径。</p>
<p><strong>OBJECT 参数说明:</strong></p>
<table style="height: 236px; width: 937px" border="0">
<tbody>
<tr>
<td style="text-align: center"><strong>参数名</strong></td>
<td style="text-align: center"><strong>类型</strong></td>
<td style="text-align: center"><strong>必填</strong></td>
<td style="text-align: center"><strong>说明</strong></td>
</tr>
<tr>
<td>url</td>
<td>String</td>
<td>是</td>
<td>下载资源的url</td>
</tr>
<tr>
<td>header</td>
<td>Object</td>
<td>否</td>
<td>HTTP请求Header,header中不能设置Referer</td>
</tr>
<tr>
<td>success</td>
<td>Function</td>
<td>否</td>
<td>下载成功后以tempFilePath的形式给页面,res={tempFilePath:'文件的临时路径'}</td>
</tr>
<tr>
<td>fail</td>
<td>Function</td>
<td>否</td>
<td>接口调用失败的回调函数</td>
</tr>
<tr>
<td>complete</td>
<td>Function</td>
<td>否</td>
<td>接口调用结束的回调函数(调用成功、失败都会执行)</td>
</tr>
</tbody>
</table>
<div class="cnblogs_code">
<pre>      const downloadTask=<span style="color: rgba(0, 0, 0, 1)">uni.downloadFile({
                url: </span>'http://www.example.com/file/test',<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">下载地址</span>
                success:<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(res){
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span>(res.statusCode===200<span style="color: rgba(0, 0, 0, 1)">){
                        console.log(</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>
            downloadTask.onProgressUpdate(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(res){
                console.log(</span>'下载进度'+<span style="color: rgba(0, 0, 0, 1)">res.progress);
                console.log(</span>'已经下载数据长度'+<span style="color: rgba(0, 0, 0, 1)">res.totalBytesWritten);
                console.log(</span>'数据总长度'+<span style="color: rgba(0, 0, 0, 1)">res.totalBytesExpectedToWrite);
               
               
            })</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/ckfuture/p/14386324.html
頁: [1]
查看完整版本: uni-app 图片上传