How to get the name of a file downloaded with Angular $http?

前端 未结 10 903
一生所求
一生所求 2021-02-01 15:36

I\'ve written code that uses Angular $http to download a file. The name of the file is not specified in the URL. The URL contains a unique identifier for the file, wh

10条回答
  •  Happy的楠姐
    2021-02-01 16:37

    There are a lot of other good answers here - here's what I ended up with that worked best for me against an ASP.NET Core 3.1 server, using a lot of these as a guide.

    function getFilename() {
        const header = response.headers.get("Content-Disposition");
        if (!header) {
            return null;
        }
    
        let matches = /filename=\"?([^;"]+)\"?;?/g.exec(header);
    
        return matches && matches.length > 1 ? matches[1] : null;
    }
    

提交回复
热议问题