I am developing a file uploader section in my app. The client side is Vue.js and the backend is PHP. Using Laravel as my framework.
I am slicing up the selected files at the client side using Blob.slice() (I have also tried FileReader Api, Resumablejs, Now working on my own implementation). Data is sent using xhr (tried Axios, XMLHTTPRequest), one request per "slice" or "chunk". I fetch the data at the backend and save the incoming file as "chunk1", "chunk2" ... and so on. Upon receiving the last chunk, I merge the chunks using PHP.
My problem is that the merged file somehow corrupts. mp4s - not playable or not seekable, exes - corrupt, some exes do good but not all (its unpredictable), some small pdfs survive.
Failed Attempts
--- save chunk with Storage::put() or Storage::putFileAs() --- save chunk with fopen(file, 'wb' or 'ab'), fwrite(), fclose() --- save chunk with file_put_contents
--- save chunk as received (base64 encoded) -> read each chunk with base64_decode() while saving data in new file --- append all chunks as received (base64 encoded) to one file -> later create a new file decoding this appended file. (this attempt was by far the most successful one but still some files corrupted, especially exes).
Client side code ...
upload(file, start = 0, request = 0) {
let chunkSize = 1024 * 1024 * 3;
let end = (start + chunkSize) > file.fileObject.size ? file.fileObject.size : (start + chunkSize);
let reader = new FileReader();
let slice = file.fileObject.slice(start, end);
reader.onload = (e) => {
let data = {
fileName: file.fileObject.name,
chunkNumber: request + 1,
totalChunks: Math.ceil(file.fileObject.size / chunkSize),
chunk: reader.result.split(',')[1]
}
axios({
url: '/api/admin/batch-sessions/'+ this.batchSessionId +'/files',
method: 'POST',
data: data,
headers: {'Content-Type': 'application/json'}
})
.then(res => {
start += chunkSize;
request++;
if (start <= file.fileObject.size) {
this.upload(file, start, request);
}
})
.catch(err => {
console.log(err.message);
});
}
reader.readAsDataURL(slice);
}
Server side code ...
public function handle()
{
$chunks = Storage::disk('s3-upload-queue')
->files($this->directory);
$mergedFile = Storage::disk('s3-upload-queue')->path($this->directory.'/'.basename($this->directory));
$base64File = Storage::disk('s3-upload-queue')->path($this->directory.'/'.basename($this->directory).'.b64');
$mfs = fopen($mergedFile, 'wb');
$b64fs = fopen($base64File, 'r');
fwrite($mfs, base64_decode(fread($b64fs, filesize($base64File))));
fclose($mfs);
fclose($b64fs);
}
Actually I do not have in-depth knowledge about different encodings, was reading about base64 chunking here on stackoverflow and tried to create "slice" of size (1024 * 1024 * 3). this is when most files were merged successfully using base64 encoded transfer. but that too was unpredictable. some files still corrupted. I am trying to understand this properly. Please let me know if more info is needed. Thanks.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Vue is an open source JavaScript framework for building a creative interface. Integration with Vue in projects using other JavaScript libraries is simplified because it is designed to be adaptable. Over 36,000 websites currently use Vue. Companies like Stackoverflow, PlayStation, etc. rely on Vue for their UI sites.
https://vuejs.org/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.