Custom name for BLOB URL

前端 未结 3 1849
后悔当初
后悔当初 2020-11-29 08:13

We have an Angular application that gets some input parameters and sends them to the back end where they get processed. The processing result is a pdf file that we want to o

相关标签:
3条回答
  • 2020-11-29 08:49

    Inside else condictional code above, insert this following code below:

    var file = new BLob([response.data), {type: 'application/pdf'});
    var fileURL = URL.createObjectURL(file);
    
    // create <a> tag dinamically
    var fileLink = document.createElement('a');
    fileLink.href = fileURL;
    
    // it forces the name of the downloaded file
    fileLink.download = 'pdf_name';
    
    // triggers the click event
    fileLink.click();
    
    0 讨论(0)
  • 2020-11-29 08:49

    I was looking for the same and actually you can add a download attribute for the link and that would make the trick on Chrome, I didn't tried in IE so far.

    <a href="urlBlobData" download="fileTest.csv">Your file</a>
    

    and this is an example with Angular 5

    <a #link *ngIf="urlBlobData" [href]="urlData" target="_blank" rel="noopener" download="fileTest.csv">Your file</a>
    

    Hope that work for you as well.

    0 讨论(0)
  • 2020-11-29 09:08

    Short answer, You can't.

    This is an address that points to the browser's memory, where it has stored your blob, or a pointer to the original file in case of user uploaded file through the input type=file.

    This is somehow by design. You can create multiple of these blobURLs from the same Blob. If they were to use a filename as URI, you couldn't.

    Theoretically, it should be possible for you to dynamically create a page that would redirect to the BlobURI, and you could name this redirection page as you which. But this is just theory, I never tried to do it myself.

    A rough proof of concept can be seen in this plunker, obviously, you'll need to generate blobRename.html dynamically, and change its name to the one you want, and also force it's content-header so that the browser thinks it's an html page if you want to get rid of the .html. Also note that it doesn't seem to work with pdf files, which need browser plugins to trigger in, but with some more work, it may be possible to hack something around.

    But anyway, I would just let the random url, your users will get more and more used to it as more and more web apps do use this great API.

    0 讨论(0)
提交回复
热议问题