I'm currently using Jhipster and have a function generated by Jhipster to open files in the browser. However, I'm facing an issue with this function when it comes to opening CSV files - instead of opening in the browser, they are being downloaded. This problem does not occur with other file types like XML.
Below is the code snippet for reference:
openFile(data: string, contentType: string | null | undefined): void {
contentType = contentType ?? '';
const byteCharacters = atob(data);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], {
type: contentType,
});
const fileURL = window.URL.createObjectURL(blob);
const win = window.open(fileURL);
win!.onload = function () {
URL.revokeObjectURL(fileURL);
};
}
If anyone has any insights or solutions to this problem, it would be greatly appreciated. Thank you!