用户指定下载位置 [英] User specified download location

查看:23
本文介绍了用户指定下载位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找了一段时间了,但找不到正确的答案.

I've been looking about for a while now and I cant find a proper answer.

基本上我有一个网页,让用户输入一些值并根据输入的值下载特定文件.一切正常,但是,我想让用户选择将文件保存到的位置,而不仅仅是浏览器保存到默认下载位置.

Basically I have a web page that lets the user enter somevalues and download a specific file depending on the values entered. All is working but, I want to enable the user to select where to save the file to, instead of just the browser saving to the default download location.

我正在使用 follownig 启动下载.

I am using the follownig to initiate the download.

 /**
        *    Get URL for data file and ensure user wants to download it
        **/
        function getResultDataHandler(result, messages) {
           var dataURL = result.value.url;

            var r = confirm("Save File?");
            if (r == true) {
                download(dataURL);
            }
            else {
                console.log("Canceled");
            }
            showMessage("", true);
        }

        /**
        *    Open the download dialog
        **/
        function download(dataURL) {
            console.log("downloading");
            window.open(dataURL, 'Download');
        }

推荐答案

浏览器是否询问用户取决于用户在浏览器中的偏好.您无法绕过这些首选项(实际上,您也不应该 —如果用户希望始终将文件下载到指定位置,则应该允许他们这样做).

Whether the browser asks the user or not is down to the user's preferences in the browser. You can't bypass those preferences (nor, really, should you — if the user wants to always download files to a specified location, they should be allowed to do that).

您可以做的是确保向浏览器发送尽可能多的信息,以帮助其提供良好的用户体验.如果您还没有这样做(您可能已经这样做了),请确保在对下载请求的响应中包含一个 Content-Disposition 标头,包括一个 filename 字段:

What you can do is make sure you're sending the browser as much information as possible to help it make for a good user experience. If you're not already doing so (you probably are), be sure to include a Content-Disposition header in the response to the download request, including a filename field:

Content-Disposition: attachment; filename=foo.csv

但我猜你已经在做那部分了.

But I'm guessing you're already doing that part.

这篇关于用户指定下载位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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