Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS如何下载txt、png文件,而不是使用window.open在新选项卡中预览_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS如何下载txt、png文件,而不是使用window.open在新选项卡中预览

Javascript AngularJS如何下载txt、png文件,而不是使用window.open在新选项卡中预览,javascript,angularjs,Javascript,Angularjs,我从后端api获取文件url,现在我想在用户单击按钮时下载它 当文件是excel(xlsx)时,它可以工作,但对于txt文件或图片(jpeg,png),它只会在新选项卡中打开,而不会下载 $scope.download = function (row) { var url = row.entity.downloadUrl;//the correct path of file server window.open(url, "_blank");/

我从后端api获取文件url,现在我想在用户单击按钮时下载它

当文件是excel(
xlsx
)时,它可以工作,但对于
txt
文件或图片(
jpeg
png
),它只会在新选项卡中打开,而不会下载

$scope.download = function (row) {
        var url = row.entity.downloadUrl;//the correct path of file server
        window.open(url, "_blank");//problem lies in here
};
如何用AngularJS制作

以下是请求

试试这个:

 var downloadLink = document.createElement("a");
 document.body.appendChild(downloadLink);
 downloadLink.style = "display: none";
 downloadLink.href = value.FilePath;
 downloadLink.download = value.FileName;
 downloadLink.click();
 downloadLink.remove();
希望这个答案能有所帮助。