danialfarid / ng-file-upload

Lightweight Angular directive to upload files with optional FileAPI shim for cross browser support
MIT License
7.87k stars 1.6k forks source link

Save the response of the file upload to another file #1961

Closed fomojola closed 7 years ago

fomojola commented 7 years ago

I have a file upload that is sending back a manipulated file: I'm trying to have the browser automatically trigger a file download and save dialog (like you'd get from a regular multipart post if the Content-Disposition header is set to "attachment; filename=". Is there a way to do this with ng-file-upload, so the response to the Upload.upload call is automatically saved by the browser or prompts the user to save?

fomojola commented 7 years ago

Never mind: done.

For future reference (without any error checking):

Upload.upload({
            url: url,
            data: { file: file }
        }).then(function(response){
                var blob = new Blob([response.data], { type: 'text/plain' }), url = $window.URL || $window.webkitURL;
                blob.name = "download.txt";
                document.location.href = url.createObjectURL(blob);
            }, function(response){});