vue打包app中,借助HTML5+API下载图片问题

1、线上图片地址(http、https):


源码:

downLoad() {

        plus.nativeUI.showWaiting('loading...');

        var that = this;

            var dtask = plus.downloader.createDownload('imgUrl', {}, function (d, status) {

              // 下载完成

              if (status == 200) {

                plus.gallery.save(d.filename, function () {//保存到相册方法

                  plus.nativeUI.closeWaiting()

                  plus.nativeUI.toast('<div>已保存到手机相册</div>',{

                    type:'richtext'

            });

                }, function () {

                  plus.nativeUI.closeWaiting()

                  plus.nativeUI.toast('<div>下载失败,请重试</div>',{

                    type:'richtext'

            });

                });

              } else {

              plus.nativeUI.closeWaiting()

                plus.nativeUI.toast('<div>下载失败,请重试</div>',{

                    type:'richtext'

            });

              }

            });

            dtask.start();

    }

2、canvas合成base64图片下载

转载:https://www.jianshu.com/p/c336fa712da9


源码:

downLoad (base64, success, error) {

  plus.nativeUI.showWaiting('loading...');

  var bitmap = new window.plus.nativeObj.Bitmap('image');  // 创建Bitmap对象

  var that = this;

  // 调用load/loadBase64Data方法加载图片

  bitmap.loadBase64Data(base64, function (i) {

that.bigmapToSave(bitmap, success, error)

  }, function (err) {

plus.nativeUI.closeWaiting()

    window.plus.nativeUI(err.message)

  })

},

// 调用保存bitmap的图片到本机系统文件,为之后相册保存提供路径

bigmapToSave (bitmap, success, error) {

  var name = this.randomString(10)

  var that = this;

  bitmap.save(`_doc/${name}.jpg`, { overwrite: false }, function (event) {

    console.log(event.target)

that.imageSaveByGallery(event.target, success, error)

  }, function (err) {

plus.nativeUI.closeWaiting()

    window.plus.nativeUI(err.message)

  })

},

// 图片保存

imageSaveByGallery (url, success, error) {

  window.plus.gallery.save(url, function (event) {

    const tarbitmap = window.plus.nativeObj.Bitmap.getBitmapById('image')

    tarbitmap.clear()

plus.nativeUI.closeWaiting()

    plus.nativeUI.toast('<div>已保存到手机相册</div>',{

                    type:'richtext'

        });

  }, function (err) {

plus.nativeUI.closeWaiting()

    plus.nativeUI.toast('<div>下载失败,请重试</div>',{

                    type:'richtext'

            });

  })

},

randomString (e) {

  var d = e || 32

  var t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'

  var a = t.length

  var n = ''

  for (var i = 0; i < d; i++) n += t.charAt(Math.floor(Math.random() * a))

  return n

},

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。