- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在 Android Lollipop 网页 View 中,我想让用户下载生成的 txt 文件:
// Store some text in a data URL.
var dataUrl = (window.URL || window.webkitURL).createObjectURL(
new Blob(["Hello world. :)"]));
// Create a link that lets the user download the text file as hello.txt.
var downloadLink = document.createElement('a');
downloadLink.setAttribute('href', dataUrl);
downloadLink.innerHTML = 'Click to download.';
// Working with David on this. I did the same thing.
// Fyi, setting the 'download' attribute just makes hitting the link
// nop, ie. do nothing at all in the web view, so I omitted it.
// - John
// Display the link.
document.getElementById('container').appendChild(downloadLink);
在Android java端,我写了一个DownloadListener尝试使用 DownloadManager 下载文件:
package com.somesideprojects;
import android.webkit.DownloadListener;
/**
* A download listener that lets users download files from the web view.
*/
public class CustomDownloadListener implements DownloadListener {
private MainActivity currentActivity;
@Override
public void onDownloadStart(
String url,
String userAgent,
String contentDisposition,
String mimeType,
long contentLength) {
android.util.Log.d("Logger",
"url : " + url +
" userAgent: " + userAgent +
" contentDisposition: " + contentDisposition +
" mimeType: " + mimeType + " contentLength " + contentLength);
android.net.Uri source = android.net.Uri.parse(url);
// Make a new request.
android.app.DownloadManager.Request request =
new android.app.DownloadManager.Request(source);
// Appears the same in notification bar while downloading.
String filename = getFilename(contentDisposition);
request.setDescription(
"This project will be saved in your downloads folder as " + filename + ".");
request.setTitle(filename);
// Add cookie on request header (for authenticated web app).
String cookieContent = getCookieFromAppCookieManager(source.getHost());
request.addRequestHeader("Cookie", cookieContent);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(
android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
// Save the file in the "Downloads" folder of SDCARD.
request.setDestinationInExternalPublicDir(
android.os.Environment.DIRECTORY_DOWNLOADS, filename);
// Get the download service and enqueue the file.
android.app.DownloadManager manager =
(android.app.DownloadManager) this.currentActivity.getApplication()
.getSystemService(android.content.Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
public String getFilename(String contentDisposition){
String filename[] = contentDisposition.split("filename=");
return filename[1].replace("filename=", "").replace("\"", "").trim();
};
public String getCookieFromAppCookieManager(String url){
android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
if (cookieManager == null) {
return null;
}
String rawCookieHeader = null;
// Extract Set-Cookie header value from Android app CookieManager for this URL
rawCookieHeader = cookieManager.getCookie(url);
if (rawCookieHeader == null) {
return null;
}
return rawCookieHeader;
};
public void setCurrentActivity(MainActivity currentActivity) {
this.currentActivity = currentActivity;
}
}
当我单击 Web View 中的链接时,会出现此 java 错误:
java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs: blob:file%3A///f566c1cf-b0b2-4382-ba16-90bab359fcc5
at android.app.DownloadManager$Request.<init>(DownloadManager.java:429)
当我在 HTTP 服务器上托管我的页面时,我遇到了同样的错误,因此该错误似乎源于 blob:
部分。
我现在有什么选择?如何下载存储在对象 URL 中的数据?最终,我想下载一个更大的 blob (~50MB)。
我可以将数据传递给注入(inject)到 addJavascriptInterface 中的对象。 ,但我必须 base64 encode my blob并在 java 端解码(因为只能传递原语和简单类型)。通过 javascript 编码为 base64 会使 Chromium 崩溃 50MB blob(调用 readAsDataURL 时出现内存不足错误)。
我还能如何从 Web View 下载 50MB 的 blob?或者将 50MB 二进制数据从 javascript 传输到 Android java?
== 用例详情 ==
我正在使用 javascript 生成一个 50MB 的视频文件 blob,mime 类型为 video/x-ms-wmv
。这一代完全是客户端。我确信该步骤有效,因为我可以通过 object URL 在桌面浏览器(以及普通的 Chrome 应用程序)上下载文件。 .然后我想以某种方式让用户将该文件存储在他/她的外部存储中(可能在 DCIM 或下载中)。
另外,我还想对音频 WAV 文件做同样的事情(比如生成铃声以存储在铃声中)。
最佳答案
大卫,我已经调查过了。你在这里有一个很好的挑战。问题是 DownloadListener 确实需要一个 URI(例如 http://server.com/file.pdf )。但是,用户单击的链接不会传递该格式的 URI。传递的 URL 表示附加到浏览器 DOM 的 blob。因此,该解决方案将无法按设计工作。
您可能还有其他选择。这取决于您如何为 blob 生成字节。您提到您正在使用 Javascript 执行此操作。如果是这样,我将完全跳过使用 Blob 和 URL.createObjectURL 并编写一个 Javascript/Native 接口(interface),它允许您将字节 block (例如,n 个 255 字节的数组)传输到 Java 代码。这将降低客户端的内存消耗。
我对如何创建接口(interface)有一个想法,但我首先需要了解如何为 blob 生成字节。如果您可以发布一个字节数组,我可以使用它来测试我的解决方案。告诉我。
关于android - 在 Lollipop Web View 中下载对象 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31113160/
当我尝试通过我的 .exe 文件从 url 下载 .pdf 文件时出现以下错误。 The server committed a protocol violation. Section=Response
我是一家非营利组织的 G Suite 管理员,刚刚发现数据导出功能,这似乎是个人帐户的外卖。 导出文件已准备好,现在可以从 Google Cloud Platform Storage 中的存储桶下载。
导航 引言 总体思路 七牛云相关的配置文件 获取七牛云上传token 相关类定义 核心代码实现 获取七牛云图片下载链接 公开空
这不是后端编程问题。我只能修改标记或脚本(或文档本身)。我在这里问的原因是因为我对适当术语的所有搜索都不可避免地导致有关编程此功能的问题和解决方案。我不是试图通过编程来强制它;我必须找出此 PDF 行
您好,我已在 Google AdSense 中注册,我想使用适用于 iOS 的 SDK,但目前我找不到 SDK 下载链接。 我的申请已获批准。 任何人都知道如何下载这个sdk。 我使用这个链接来描述如
我需要为当前在 SourceForge 上的 github 项目提供二进制文件和文档。在那里,我可以为我需要的下载提供一个目录结构,因为我必须为大约 10 个不同的操作系统提供几个版本。 github
我从 Canvas 下载绘图时遇到问题。这是我的代码: function downloadCanvas(link, canvasId, filename) { link.href =
ASP.NET 项目 我将使用 Azure 进行存储。问题(要求): 在我的项目中,我让注册用户下载文件。但我不希望用户将此下载链接分享给未注册的人(例如:我给注册用户的下载链接只能在他们的计算机上下
我编写了一个servlet,用于检查http header ,但我不知道为什么当页面加载时,它会自动开始下载。 /* * To change this template, choose To
我正在尝试将下载添加到我的网络浏览器,但遇到的问题是获取您尝试下载的文件的名称。这是我的下载代码: engine.locationProperty().addListener(new ChangeLi
我正在尝试下载网站的 html: String encoding = "UTF-8"; HttpContext localContext = new BasicHttpContext();
我制作了一个带有“开始下载”按钮的框架,用于从网站下载 JAR。 问题是每当我点击开始下载按钮时,整个框架就会卡住,直到下载完成,然后就正常了。 我该如何解决这个问题? 这是单击按钮时执行的代码 p
我得到这段代码来实现一些东西,它可以帮助我从给定的 URL 下载文件。 -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSes
我正在尝试创建一个 Controller 来使用流方法下载和上传文件,在我的例子中,所有文件都作为 Blob 保存在数据库中。我阅读了 Jboss Netty 的文档,但我认为这不是我的最佳解决方案。
下载并保存文件 let destination: DownloadRequest.DownloadFileDestination = { _, _ in // var fileURL = sel
使用 htaccess 我基本上试图禁止访问该页面,即 http://example.com , 但它仍然允许人们下载文件,如果他们有直接链接即 http://example.com/hi.zip .
我正在寻求将脚本与我的控制面板集成,并且由于我是新手脚本编写者而遇到问题。我想做的是用 1 个脚本下载一个文件并解压它。 示例: wget http://example.com/example.tar
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
这个问题在这里已经有了答案: Top techniques to avoid 'data scraping' from a website database (14 个答案) 关闭 5 年前。 我有
这个问题在这里已经有了答案: Reading and parsing email from Gmail using C#, C++ or Python (6 个答案) 关闭 7 年前。 我只是想,是
我是一名优秀的程序员,十分优秀!