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

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

问题描述

我一直在寻找一段时间,我找不到正确的答案。



基本上我有一个网页,让用户输入一些价值和下载具体文件取决于输入的值。
全部正在工作,但是我想让用户选择保存文件的位置,而不是将浏览器保存到默认的下载位置。



我正在使用follownig开始下载。

  / ** 
*获取数据文件的URL,并确保用户想下载它
** /
函数getResultDataHandler(result,messages){
var dataURL = result.value.url;

var r = confirm(保存文件?);
if(r == true){
下载(dataURL);
}
else {
console.log(Cancelled);
}
showMessage(,true);
}

/ **
*打开下载对话框
** /
函数下载(dataURL){
console.log 下载);
window.open(dataURL,'下载');
}


解决方案

浏览器是否要求用户或者不符合用户在浏览器中的偏好。您不能绕过这些偏好(如果用户希望始终将文件下载到指定的位置,那么应该允许这样做)。



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

 内容处理:附件;文件名= foo.csv 

但我猜你已经在做这个部分。 >

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.

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).

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天全站免登陆