Im sorry if my question is a bit confusing but I think can clarify this by my detailed explanation.
Im very new to saving/uploading files. I have recently been able to upload image and save it to database as base64
(datatype = mediumblob
) and display it again in the browser by setting <img src='base64_data_from_db' />
.
Now for my actual problem.
I have to upload file of any type to the database. After saving it, the user must be able to download the file using the browser by clicking a link/button (I used link). Im able to save the file using base64.
The problem I have right now is that how can I convert the base64 string to the actual/original file (like MS Word, text file, image.. etc) and download it to the user.
I removed the prefix of the base64 string and used window.atob(str)
to
convert it. I can see somehow the contents of the file when I console.log(window.atob(str))
but I don't know how to download it.
NOTE: I used base64 when saving because I save it using JSON.
Im using Java Spring Hibernate.
I tried to google but most of the time I see how to convert Base64 to image
... As I have said earlier I have already displayed the image in <img src=''/>
. Im also reading about javascript FileReader but it requires a Blob as parameter and what I have is a base64 string.
I have a very incomplete code below because I don't know what to do next...
// strBase64 -- The base64 string equivalent of the file
function convBase64ToFile(strBase64) {
var tmp = strBase64.split(","); // To remove the prefix
var converted = window.atob(tmp[1]); //
console.log(converted); // Displays the content of the actual file
}
Please help....