微信小程序点击单个图片进行预览图片,点击预览pdf文件

待我称王封你为后i 2022-12-10 08:55 998阅读 0赞

这里介绍的是单个图片点击预览的,也可以多张,方法差不多
index.wxml

  1. <view class="page">
  2. <view class="page__hd">
  3. <text class="page__title">image</text>
  4. <text class="page__desc">图片</text>
  5. </view>
  6. <view class='imgList'>
  7. <view class='imgList-li'>
  8. <image src="{ {ItemUrl}}{ {imgurl}}" mode="scaleToFill" bindtap='previewImg'></image>
  9. </view>
  10. </view>
  11. </view>

index.wxss

  1. .imgList{
  2. width: 100%;
  3. }
  4. .imgList .imgList-li{
  5. width: 300rpx;
  6. height: 300rpx;
  7. }
  8. .imgList .imgList-li image{
  9. width: 300rpx;
  10. height: 300rpx;
  11. }

index.js

  1. Page({
  2. data: {
  3. imgurl :'/dmsmty/74_74_100/t01df7265f2a34fadb9.png',
  4. ItemUrl: 'https://p.ssl.qhimg.com'
  5. },
  6. previewImg:function(e){
  7. var imgArrUrl = this.data.ItemUrl + this.data.imgurl;
  8. wx.previewImage({
  9. current: imgArrUrl, //当前图片地址
  10. urls: imgArrUrl.split(","), //所有要预览的图片的地址集合 数组形式['https://p.ssl.qhimg.com/dmsmty/74_74_100/t01df7265f2a34fadb9.png']
  11. success: function(res) { },
  12. fail: function(res) { },
  13. complete: function(res) { },
  14. })
  15. }
  16. })

如果是点击预览pdf文件(真机华为手机可能有问题)

  1. wx.downloadFile({
  2. url: 'https://storage.jd.com/eicore-fm.jd.com/042001900211-71246106.pdf?Expires=2538379555&A', //要预览的PDF的地址
  3. success: function (res) {
  4. console.log(res);
  5. if (res.statusCode === 200) { //成功
  6. var Path = res.tempFilePath //返回的文件临时地址,用于后面打开本地预览所用
  7. wx.openDocument({
  8. filePath: Path, //要打开的文件路径
  9. success: function (res) {
  10. console.log('打开PDF成功');
  11. }
  12. })
  13. }
  14. },
  15. fail: function (res) {
  16. console.log(res); //失败
  17. }
  18. })

发表评论

表情:
评论列表 (有 0 条评论,998人围观)

还没有评论,来说两句吧...

相关阅读