Read, edit the data and download multiple files in pure JavaScript - javascript

Firstly, I allow users to upload multiple files. Then I use loop with fileReader to read all the uploaded files. Next I edit some data of each file and create new files contain each data. Finally, I want to download all of these files using fileSaver library. The problem is that I can only download the last file with the below code. I would be very grateful if someone can fix it.
Code:
<html>
<body>
<!-- Link to FileSaver Library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js" integrity="sha512-Qlv6VSKh1gDKGoJbnyA5RMXYcvnpIqhO++MhIM2fStMcGT9i2T//tSwYFlcyoRRDcDZ+TYHpH8azBBCyhpSeqw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<!-- Allow users to upload multiple files -->
<input type="file" id="real-file" accept=".xml" multiple/>
<script>
const realFileBtn = document.getElementById("real-file")
realFileBtn.addEventListener("change", function() {
if (realFileBtn.value) {
// Use loop to do for all uploaded files
for(var i = 0; i < realFileBtn.files.length; i++){
var fr = new FileReader()
var fileName = realFileBtn.files[i].name
fr.readAsText(realFileBtn.files[i])
fr.onload = function() {
// After loading the file, I get the data of it and edit some information (for example: replace "a" with "b")
var data = fr.result.replaceAll("a", "b")
// I create a new file that contain the edited data with the same name as the uploaded file
var myFile = new File(
[data],
fileName,
{type: "text/xml; charset = utf8"})
// Then I download that file
saveAs(myFile)
// The issue is that I can only download 1 file and that file is the last file you uploaded
// I want to read the first file, edit its data and download the file contain that data. Then do the same with second file,
// third file,... n file
}
}
}
})
</script>
</body>

Related

Angular8 - Downloading Excel .xlsx saves base64 string in file

I've studied all of the SO Similar Questions about this problem, a few were helpful, but did not directly address the saving of an xlsx file to the local Win10 downloads folder.
The xlsx file sits on a CentOS 7 server. If I use Filezilla to copy it to Win10, it's fine.
Looking at the component code below, I am reading the xlsx from the server and getting its base64Str, which matches exactly what I used to store the file. I create a blob fine, but FileSaver, saves the Win10 file with its contents as the base64Str, instead of a an xlsx binary. Can FileSaver save binary content, or only text?
Thanks for helping!
Component Code
//INPUT DATA: docName: test.xlsx; mediaType: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; snippet base64Str: VUVzREJCUU...FBQUFBPQ=
//NOTE: input data is from a CentOS 7 filesystem
const byteStr = atob(base64Str);
const arrayBuffer = new ArrayBuffer(byteStr.length);
const int8Array = new Uint8Array(arrayBuffer);
for (let i = 0; i < byteStr.length; i++) {
int8Array[i] = byteStr.charCodeAt(i);
}
const blob = new Blob([arrayBuffer], { type: mediaType }); //also tried type: 'application/octet-stream'
//alternate technique that produces same corrupted result
//const base64Response = await fetch(`data:` +mediaType +`;base64,${base64Str}`);
//const blob = await base64Response.blob();
fileSaver.saveAs(blob, docName); //saves to Win10 downloads folder
Please try The below code. (Here in this example I have demonstrate the following 1) Input base64 string to a input text area 2) on click of download file link 3) will prompt to download the file into correct xlsx format without corrupted data).
Kindly modify the code as per your need . Example only demonstrate the file download feature.
Point to notice :
var mediaType="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,";
function saveAsXlsxFile(){
var mediaType="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,";
var userInp = document.getElementById('base64input');
// var userInp = "UEsDBBQABgAIAAAAIQBBN4LPbgEAAAQFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AMYgAAATAAAAAAAAAAAAAAAAAEoPAAB4bC90aGVtZS90aGVtZTEueG1sUEsBAi0AFAAGAAgAAAAhACBSf139AgAAwgcAAA0AAAAAAAAAAAAAAAAAyRYAAHhsL3N0eWxlcy54bWxQSwECLQAUAAYACAAAACEAw//qhdEAAACPAQAAFAAAAAAAAAAAAAAAAADxGQAAeGwvc2hhcmVkU3RyaW5ncy54bWxQSwECLQAUAAYACAAAACEAO20yS8EAAABCAQAAIwAAAAAAAAAAAAAAAAD0GgAAeGwvd29ya3NoZWV0cy9fcmVscy9zaGVldDEueG1sLnJlbHNQSwECLQAUAAYACAAAACEAo9Y9cDYBAADkAgAAJwAAAAAAAAAAAAAAAAD2GwAAeGwvcHJpbnRlclNldHRpbmdzL3ByaW50ZXJTZXR0aW5nczEuYmluUEsBAi0AFAAGAAgAAAAhAFSTEGpDAQAAawIAABEAAAAAAAAAAAAAAAAAcR0AAGRvY1Byb3BzL2NvcmUueG1sUEsBAi0AFAAGAAgAAAAhAGFJCRCJAQAAEQMAABAAAAAAAAAAAAAAAAAA6x8AAGRvY1Byb3BzL2FwcC54bWxQSwUGAAAAAAwADAAmAwAAqiIAAAAA";
var a = document.createElement('a');
a.href = mediaType+userInp.value;
//a.href = mediaType+userInp;
a.download = 'filename.xlsx';
a.textContent = 'Download file!';
document.body.appendChild(a);
}
<textarea placeholder="paste the base64 data here, exclude readable headers" id='base64input' type="textarea"></textarea>
<br>
<input onclick="saveAsXlsxFile()" type="button" value="update content of link"></button>
HTML code for demo only ( not mandatory for your requirement)
<textarea placeholder="paste the base64 data here, exclude readable headers" id='base64input'></textarea>
<br>
<input onclick="saveAsXlsxFile()" type="button" value="update content of link"></button>
Sample userInp value Try this :
UEsDBBQABgAIAAAAIQBBN4LPbgEAAAQFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAACnAwAAX3JlbHMvLnJlbHNQSwECLQAUAAYACAAAACEARbo8GF0DAABaCAAADwAAAAAAAAAAAAAAAADMBgAAeGwvd29ya2Jvb2sueG1sUEsBAi0AFAAGAAgAAAAhAIE+lJfzAAAAugIAABoAAAAAAAAAAAAAAAAAVgoAAHhsL19yZWxzL3dvcmtib29rLnhtbC5yZWxzUEsBAi0AFAAGAAgAAAAhAAdhSeKLAgAARAYAABgAAAAAAAAAAAAAAAAAiQwAAHhsL3dvcmtzaGVldHMvc2hlZXQxLnhtbFBLAQItABQABgAIAAAAIQDBFxC+TgcAAMYgAAATAAAAAAAAAAAAAAAAAEoPAAB4bC90aGVtZS90aGVtZTEueG1sUEsBAi0AFAAGAAgAAAAhACBSf139AgAAwgcAAA0AAAAAAAAAAAAAAAAAyRYAAHhsL3N0eWxlcy54bWxQSwECLQAUAAYACAAAACEAw//qhdEAAACPAQAAFAAAAAAAAAAAAAAAAADxGQAAeGwvc2hhcmVkU3RyaW5ncy54bWxQSwECLQAUAAYACAAAACEAO20yS8EAAABCAQAAIwAAAAAAAAAAAAAAAAD0GgAAeGwvd29ya3NoZWV0cy9fcmVscy9zaGVldDEueG1sLnJlbHNQSwECLQAUAAYACAAAACEAo9Y9cDYBAADkAgAAJwAAAAAAAAAAAAAAAAD2GwAAeGwvcHJpbnRlclNldHRpbmdzL3ByaW50ZXJTZXR0aW5nczEuYmluUEsBAi0AFAAGAAgAAAAhAFSTEGpDAQAAawIAABEAAAAAAAAAAAAAAAAAcR0AAGRvY1Byb3BzL2NvcmUueG1sUEsBAi0AFAAGAAgAAAAhAGFJCRCJAQAAEQMAABAAAAAAAAAAAAAAAAAA6x8AAGRvY1Byb3BzL2FwcC54bWxQSwUGAAAAAAwADAAmAwAAqiIAAAAA
Happy to help further if needed. :)
so I used this. Might also work for anyone trying to convert a static base 64 string to xlxs file. This solutions assumes that you know the file type.
let toConvert = 'UEsDBBQAAAgIABSJA..'//supply your full plain string here
let file = `data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,${toConvert}`;
setTimeout(() => {
fileSaver.saveAs(file, 'fileName');
}, 1300);//timeout is only added to simulate time to load. you can take it out
Regards
Not sure if this works , but try setting the mediaType like below :
const mediaType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';

jQuery get fakepath for file input with "multiple" attribute to preview image

I have input file (Multiple) use to upload images. When an image is uploaded it will display as a thumbnail and also as a full view. I already have URL.createObjectURL() that renders the image into blob but when the number of images is more then it is effecting the page a little since each image is having 2 blob-data for thumbnail and full view.
For single file upload it was easy to have URL.createObjectURL() for thumbnails and $(this).val() to generate fake path for full view. But I do not know how to do that with multi file upload.
Sample code:
$('#imageBtn').on('change', function(){
var inputFiles = this.files;
$(inputFiles).each(function(index) {
var reader = new FileReader();
reader.readAsDataURL(inputFile);
reader.onloadend = function(){
RenderedPath = URL.createObjectURL(inputFile);
FakePath = inputFile.val();
// Some codes to populate the thumbnail and fullview
}
});
});
So, how can I obtain the fake path for each uploaded image?
I don't see what you want to do with this fakePath. Being a "fake" path, it's of no use.
Historically, (before the File API), it was a way to retrieve the name of a selected File, but now that we've got the File API, this information is provided inside the File object directly.
So if you really want to build it yourself, like in the case of an input[type=file][multiple], then you can simply prepend "C:\fakePath\" to the File's name.
But once again, you won't be able to do anything from this string.
Also note that in your code you don't do anything with the FileReader's result, and anyway, you don't need it at this point, so remove it here, since it is probably one of the causes of your memory issues.
Indeed, BlobURIs in the case of user-provided Files won't use any memory, being simple pointers to the file stored on user-disk, while reading it as a dataURI will actually load the whole data three times in memory.
So for the part you shown, it can simply be
$('input[type="file"]').on('change', function() {
var files = this.files;
$(files).each(function(index, file) {
// Still don't know why you want this...
var fakepath = 'C:\\fakepath\\';
$('body').append('<div>' +
// build a fake path string for each File
'<p class="path">'+ fakepath + file.name + '</p>' +
// all that is really needed to display the image
'<img src="'+URL.createObjectURL(file)+'">' +
'</div>');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" multiple>
Now, you stated in a comment that you need the FileReader to pass it to jsZip library, if you are talking about this one, then you've to know that it accepts Blobs. So you still don't need a FileReader.
var fileList = [];
$('input[type="file"]').on('change', function() {
var files = this.files;
$(files).each(function(index, file) {
$('body').append($('<img>', {
src: URL.createObjectURL(file),
onload: function() { fileList.push(file); $('button').attr('disabled', false);},
onerror: function() { $(this).remove(); }
}));
});
});
$('button').on('click', function() {
var zip = new JSZip();
fileList.forEach(function(file) {
zip.file(file.name, file);
});
zip.generateAsync({
type: "blob"
})
.then(function(blob) {
saveAs(blob, "myfiles.zip");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js"></script>
<input type="file" multiple accept="image/*">
<button disabled>save as zip</button>

Removing large files from file upload

I would like to allow a user to upload multiple files through Ajax. If some of the files are too large I would like to remove them from the upload list. How can I go about doing this? I currently have the following for my input field.
<input id="file" type="file" onChange={this.handleChange.bind(this)} required multiple />
So far I have come up with something like this to verify which files are too large:
handleChange(event) {
const target = event.target;
if (target.type === 'file'){
console.log("FILES")
const files = target.files
console.log("The following files are too large")
for (let i=0; i < files.length; i++){
if (files[i].size > 50000){
console.log(files[i].name)
}
}
}
}
The thing I am confused about is how files are handled, as they are not really links to the actual files on the file system. How does the file structure work? How can I store the good files into a new file structure that is uploadable with Ajax?
target.files is just an array. You can simple filter it, removing the files you don't want and do whatever you want with the new array (e.g. store it in the component's state).
const files = target.files.filter(file => file.size <= 50000);
this.setState({files});
// or uploadFiles(files)
If you are asking how to actually send files via an XMLHTTPRequest, this can be done with FormData:
const data = new FormData();
files.forEach(file => data.append(file.name, file));
and then send the FormData object via the XMLHTTPRequest.

How is a file split / chunked? How do I access the chunks sequentially?

For example, if I have an image and I download it -- how does the computer know that all the bytes are supposed to be in that sequential order?
When I upload an image, is it possible to upload it in "chunks" so I can access/use whatever I have uploaded thus far (i.e. I have only uploaded the top half of the image) -- how would I access it?
The same would go for video or PDF, etc.
You can read file into chunks and upload file in chunks.
See the example below.
<input type="file" name="filebrowsefileid">
var filePicker = document.getElementById('filebrowsefileid');
var file = filePicker.files[0];
var chunck = file.slice(a,b);
var reader = new FileReader();
reader.readAsBinaryString(chunck );
....
reader["onloadend"] = function () {
reader.result; // This is what you want
}

Downloading multiple files into a Zip file Javascript using data URI

I am using EXT JS 4.2 which has a panel which contains a export to CSV button.
On clicking on it multiple (total six) files are downloaded. I want these files to be downloaded in a single ZIP file.
There is a perfect plugin to create zip files inside browser.
JSZip: https://stuk.github.io/jszip/
Install the plugin by adding js files manually:
download JSZip and include the file dist/jszip.js or
dist/jszip.min.js
JSFiddle - JSZip 3.0:
https://jsfiddle.net/andrebonna/u8zsbzau/
var zip = new JSZip();
for (var i = 0; i < 5; i++) {
var CSV = 'CSV_content';
// Fill CSV variable
zip.file("file" + i + ".csv", CSV);
}
zip.generateAsync({
type: "base64"
}).then(function(content) {
window.location.href = "data:application/zip;base64," + content;
});

Categories

Resources