swan.downloadFile

    Object object

    success 返回参数说明 :

    • Android
    • iOS

    扫码体验

    请使用百度APP扫码

    swan.downloadFile - 图3

    代码示例1 :

    • 在 js 文件中

    • 在 js 文件中
    1. data: { },
    2. downloadFile() {
    3. url: 'https://smartprogram.baidu.com/docs/img/file-simple.pdf',
    4. header: {
    5. 'content-type': 'application/json'
    6. },
    7. filePath: 'bdfile://usr/办理指南文档.pdf',
    8. success: res => {
    9. console.log(res);
    10. let filePath = res.filePath;
    11. title: '文件下载完成',
    12. content: '是否需要打开?',
    13. confirmText: '打开',
    14. success: res => {
    15. if (res.confirm) {
    16. swan.openDocument({
    17. filePath: filePath,
    18. fileType: 'pdf',
    19. this.toast('打开失败');
    20. }
    21. }
    22. }
    23. });
    24. },
    25. fail: err => {
    26. this.toast('下载文件失败');
    27. },
    28. complete: () => {
    29. swan.hideToast();
    30. }
    31. });
    32. });

    返回值:

    • 文件的临时路径,在智能小程序本次启动期间可以正常使用,如需持久保存,需再主动调用 swan.saveFile,才能在智能小程序下次启动时访问得到;
    • 下载最大限制10MB。