I would like to create a SAPUI5 application in which an HTML file can be uploaded. The contents of the file should be read in the next step. After the header tag, an additional text will be inserted in the content and the new file will be made available for download. To make it more clear:
Any ideas?
It is fairly easy to do this with pure UI5, without needing to use something like WebIDE.
You need to use three classes in particular:
Assuming that you don't want to do a backend round-trip for reading the file, you can also use the FileReader API.
So these are the steps that you need to make:
<u:FileUploader change="onChangeFile" />
onChangeFile: function(oEvent) {
var oModel = this.getView().getModel();
var oReader = new FileReader();
oReader.onload = function() {
oModel.setProperty("/code", oReader.result);
}
oReader.readAsText(oEvent.getParameter("files")[0]);
},
<Button text="Download" press="onDownload" />
<ce:CodeEditor type="html" value="{/code}" height="300px" />
onDownload: function() {
var sText = this.getView().getModel().getProperty("/code");
File.save(sText, "download", "html");
}
You can find a working fiddle of this here: https://jsfiddle.net/5z7deeyd/1/.
Later edit:
If you just need to change the file without user interaction, then you only need steps 1 and 2. For step 2, the handler can look like this:
onChangeFile: function(oEvent) {
var oModel = this.getView().getModel();
var oReader = new FileReader();
oReader.onload = function() {
// do something with the oReader.result...
var sNewContent = oReader.result + "something";
File.save(sNewContent, "download", "html");
}
oReader.readAsText(oEvent.getParameter("files")[0]);
}