javascript - HTML anchor 标记无法在 Android 平台中下载文件

标签 javascript android html visual-studio-2013 download

首先,我尝试使用 Visual Studio 2013 从本地服务器下载文件来开发移动应用程序。

我使用此代码下载文件。单击页面时它工作正常,但当我尝试在 Android 上下载文件时它没有响应。 (无法将 Android 的目标下载路径放入 anchor 标记中)

有没有办法使用 HTML( anchor 标记)或 JavaScript 下载文件?

代码:

<a id='download' href='path_to_file' download><img class='thumb' src="url" /></a>

最佳答案

您可以使用 JavaScript 进行更多控制。此代码 fragment 创建一个“不可见” anchor 元素并单击它。运行该代码 fragment 将下载山羊的图片。

var myURL = 'http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2014/4/11/1397210130748/Spring-Lamb.-Image-shot-2-011.jpg'
handleAndroidMobile(myURL);

function handleAndroidMobile(myURL) {
  var link = document.createElement('a');
  link.download = 'Downloaded_file_name';
  link.href = myURL;
  link.click();
}

我不知道如何指定路径。如果我找到解决方案,我会更新此答案。

关于javascript - HTML anchor 标记无法在 Android 平台中下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29528974/

相关文章:

android - 如何在 Android 中使用 Retrofit 库获取和发布方法?

Edge 中的 HTML 提交按钮已断开边框,但插入边框正确

android - 循环时听按钮

html - 如何获得最小宽度或类似的东西以在整个页面上工作? IE7 和 8

html - 错误 : object was provided as `children` instead of a component, 字符串或数字(或它们的列表)

当页面第一次加载时,javascript设置ipad的绝对位置

javascript - 过滤 jQuery 选择的返回数组的最快方法

javascript - ASI 是什么意思?

javascript - 为什么我需要为 karma 配置指定所有文件,即使它们是用 requirejs 加载的

android - 动态改变状态栏颜色而不失去透明度