windowSaveAsFile = function filename bytesBase64 var link = documentcreateElementa; linkdownload = filename; linkhref = dataapplicationoctet-stream;base64 + bytesBase64; documentbodyappendChildli
This JavaScript code defines a function called SaveAsFile that allows you to save a file on the client-side using a specified filename and base64-encoded file content.
Here's how it works:
- It creates an
<a>element using thedocument.createElementmethod. - It sets the
downloadattribute of the<a>element to the specifiedfilename. - It sets the
hrefattribute of the<a>element to a data URL that represents the file content. The data URL is constructed by appending thebytesBase64(base64-encoded file content) to thedata:application/octet-stream;base64,prefix. - It appends the
<a>element to the<body>element using theappendChildmethod. - It triggers a click event on the
<a>element using theclickmethod. - It removes the
<a>element from the<body>element using theremoveChildmethod.
This code allows you to dynamically generate a file on the client-side and prompt the user to save it with the specified filename. The file content should be provided as a base64-encoded string in the bytesBase64 parameter
原文地址: http://www.cveoy.top/t/topic/iv5P 著作权归作者所有。请勿转载和采集!