简体   繁体   English

如何使用 javascript 在文档根目录外下载图像

[英]How to download an image outside the document root with javascript

Our images are stored out of the document root, therefor we use a script to get it into the browser.我们的图像存储在文档根目录之外,因此我们使用脚本将其导入浏览器。

/prx/asset.php?tgt=<photo_filename>

I can display correctly the image in the web我可以正确显示 web 中的图像

$("#photo-editmodal").attr('src', '../prx/asset.php?tgt='+details.photo_saved_file);

The goal is be able to download that image.目标是能够下载该图像。

I have been reading some references and the closest point I have achieved is doing a request through AJAX and then create an ObjectURL with the result, but (I think) since the stream data received, it is not a valid reference to create the ObjectURL.我一直在阅读一些参考资料,并且我取得的最接近的一点是通过 AJAX 发出请求,然后使用结果创建一个 ObjectURL,但是(我认为)自从收到 stream 数据以来,它不是创建 ObjectURL 的有效参考。

Here it is my try:这是我的尝试:

                $.ajax({
                    url: '../prx/asset.php?tgt='+details.photo_saved_file,
                    method: 'GET',
                    xhrFields: {
                        responseType: ''
                    },
                    success: function (data) {
                        console.log(data);

                        var a = document.createElement('a');
                        var url = window.URL.createObjectURL(data);

                        a.href = url;
                        a.download = details.photo_saved_file;
                        document.body.append(a);
                        a.click();
                        a.remove();
                        window.URL.revokeObjectURL(url);
                    },
                    // error: function(data) {
                    //     console.log(data);
                    // }
                });

I get the message:我收到消息:

Uncaught TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.

Anyone can help to get this done?任何人都可以帮助完成这项工作吗?

EDIT编辑

I get rid of the ajax call and now I am doing the following:我摆脱了 ajax 调用,现在我正在执行以下操作:

$("#download_photo").attr('href', '../prx/asset.php?tgt='+photo);
$("#download_photo").attr('download', photo);    

The issue I have now is it works on the scond time the button is clicked.我现在遇到的问题是它在第二次单击按钮时起作用。 Any idea?任何想法?

EDIT 2 It was much simpler than expected, still don't know why the link element can't be created with the help of ObjectURL, but I can assign the href when the "edit" modal is open and therefor the link with download attribute works at first click.编辑 2它比预期的要简单得多,仍然不知道为什么无法在 ObjectURL 的帮助下创建链接元素,但我可以在“编辑”模式打开时分配 href,因此链接具有下载属性在第一次点击时工作。

It was much simpler than expected, still don't know why the link element can't be created with the help of ObjectURL, but I can assign the href when the "edit" modal is open and therefor the link with download attribute works on first click.它比预期的要简单得多,仍然不知道为什么无法在 ObjectURL 的帮助下创建链接元素,但是我可以在“编辑”模式打开时分配 href,因此具有下载属性的链接可以正常工作第一次点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM