1// Copyright (C) 2023 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Initiate download of a resource identified by |url| into |filename|. 16export function downloadUrl(fileName: string, url: string) { 17 const a = document.createElement('a'); 18 a.href = url; 19 a.download = fileName; 20 a.target = '_blank'; 21 document.body.appendChild(a); 22 a.click(); 23 document.body.removeChild(a); 24 URL.revokeObjectURL(url); 25} 26 27// Initiate download of |data| a file with a given name. 28export function downloadData(fileName: string, ...data: Uint8Array[]) { 29 const blob = new Blob(data, {type: 'application/octet-stream'}); 30 const url = URL.createObjectURL(blob); 31 downloadUrl(fileName, url); 32} 33