Skip to content
Advertisement

MySQL – save base64 encoded data to storage drive

I’m building an app that uses a temp table where chunks of a file are uploaded to, then a stored proc is called with a function that reassembles the file into the right order. I’m just wondering if there’s anything in SQL that would allow me to outright save the reassembled file to disk (eg. c:/path/to/file/filename), or if I need to make the app read the reassembled file returned from the stored proc and save the file.

Either way gives me the end result I need, just wondering if I can make the database do all the work instead… or if that’s even a good idea or not.

EDIT: I chose a different method. Nothing is saved to the database. Files are reassembled at the final storage location on the server. Solution below.

Advertisement

Answer

Figured I’d update this post with my solution:

I generated a GUID client-side for each file being uploaded and use it as the filename for each chunk and prepended by chunk order ID so the server knows in which order the file needs to be re-assembled. Each chunk is posted to its final storage location on the server. I compare the total chunks to be uploaded to the total chunk count on the server, and detected when the last chunk is received, then re-compile the file to its original. Any validation needed is done after the uploaded file is re-assembled. File and any chunks are deleted if validation or upload fails at any time.

Code below edited and untested from my original. This could stand to be cleaned up some…

Not shown here is the client-side JavaScript which uses the FileReader API to post chunks in base64.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement