不幸的是,浏览器希望通过 ssl 提供所有已加载的资源。在您的情况下,您别无选择,只能自行存储所有图像或创建或代理从 http 到 https 的请求。但我不确定这样做是否真的安全。
例如,您可以执行以下操作:
我假设代码是 php,并且通过 https
<?php
define('CHUNK_SIZE', 1024*1024); // Size (in bytes) of tiles chunk
// Read a file and display its content chunk by chunk
function readfile_chunked($filename, $retbytes = TRUE) {
$buffer = '';
$cnt = 0;
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, CHUNK_SIZE);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
$filename = 'http://domain.ltd/path/to/image.jpeg';
$mimetype = 'image/jpeg';
header('Content-Type: '.$mimetype );
readfile_chunked($filename);
Credit for code sample
_ 更新 1 _
Alternate solution to proxify steamed downloaded file in Python
_ 更新 2 _
在以下代码中,您可以将数据从远程服务器流式传输到前端客户端,如果您的 Django 应用程序通过 https,则内容将正确传递。
目标是按 1024 位组读取您的原始图像,然后将每个组流式传输到您的浏览器。当您尝试加载重图像时,此方法可避免超时问题。
我建议您添加另一层以具有本地缓存而不是下载 -> 每个请求的代理。
import requests
# have this function in file where you keep your util functions
def url2yield(url, chunksize=1024):
s = requests.Session()
# Note: here i enabled the streaming
response = s.get(url, stream=True)
chunk = True
while chunk :
chunk = response.raw.read(chunksize)
if not chunk:
break
yield chunk
# Then creation your view using StreamingHttpResponse
def get_image(request, img_id):
img_url = "domain.ltd/lorem.jpg"
return StreamingHttpResponse(url2yield(img_url), content_type="image/jpeg")