[Vuejs]-How to download .js file by JavaScript from an url?

1👍

new Blob([res.data.path]) creates a Blob (which is sort-of-like-a-file) containing the text in the string you pass it.

Since that text is a URL, the file you download is a text file containing that URL.

If you want to create a Blob containing the JavaScript source code, then you need to get the JS source code. Make an HTTP request to the URL (e.g. with fetch) and put the response body in the Blob.

(Aside: don’t append .js to the generated URL with you set href, that modifies the contents of the file!)

This will, of course, require permission from CORS if this is a cross-origin request.

If it isn’t a cross-origin request then you can just set the href attribute to res.data.path without leaping through all these hoops.

Leave a comment