仅从客户端检测下载开始/文件传输的开始 [英] Detect start of download start / file transfer from client side only

查看:84
本文介绍了仅从客户端检测下载开始/文件传输的开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在询问如何
之前有一个问题检测当浏览器收到文件下载时。

I noticed that there is a question before asking how to "Detect when browser receives file download".

这要求服务器发送cookie以完成这些步骤。
但是,我想检测浏览器是否仅在客户端接收文件,因为我无法修改服务器端的任何内容。

This requires the server to send a cookie in order to complete the steps. However, I want to detect if the browser receives file only on client side, as I cannot modify anything on the server side.

我遇到的问题是我必须从网站下载许多zip文件。 zip文件是动态生成的,因此在我提交请求之前没有要下载的URL。首先,我写一个for循环来执行以下操作。

The problem I am encountering is that I have to download many zip files from a website. The zip files are dynamically generated so there is no URL to download until I submit the request. First, I write a for loop to do that as follows.

function myfunc(i){
    setTimeout(function(){
    $("form").submit();
    },2000*(i)
}
for (i=1; i<4; i++) {
myfunc(i);
} //i

但是有在浏览器收到第一个文件(i = 1)之前,浏览器已经提交了请求第二个文件的表单(i = 2)并取消第一个文件的请求。所以我必须确保在请求第二个文件之前第一个文件已经开始。

But there is a chance that before the browser receives the first file (i=1), the browser will have already submitted the form requesting the second file (i=2) and cancel the request of first file. So I have to ensure the first file has started before requesting the second file.

ajax 似乎不是一个选项,因为它似乎无法处理zip文件下载。

ajax seems not to be an option as it seems to be unable to handle zip file download.

我该怎么做?

推荐答案

编辑:
您还可以通过ajax调用获取base64 zip的字符串,然后客户端将其转换为html5中的blob。但这需要base64编码文件服务器端,它增加文件大小约为33%。

You can also ise an ajax call to get a base64 string of the zip and then clientside convert it to a blob in html5. But that requires to base64 encode the file server side and it increases the file size by around 33%.

我会进行ajax调用,检查文件是否已在启动后生成使用计时器下载:

I would make an ajax call that checks is the file is already generated after starting the download with a timer:

$.ajax({
    type: 'HEAD',
    url: 'generatedfileurl',
    success: function() {
        alert('File found.');
    },
    error: function() {
        alert('File not found.');
    }
});

然后当找到文件时我会等几秒钟然后开始下载nr2因为我假设该文件nr1已经下载,因为它已经生成了。

Then when the file is found I'd wait a few seconds and then start download nr2 since I assume that file nr1 is already downloading since it has already been generated.

这远非理想,我绝对建议使用cookies或其他服务器。

It's far from ideal, I'd definitely recommend using cookies or something else serverside.

这篇关于仅从客户端检测下载开始/文件传输的开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆