123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- var dirSelect = document.getElementById("dir");
- function getDir() {
- return dirSelect.value ? parseInt(dir.value) : null;
- }
- function arrayBufferToBase64(buffer, callback) {
- var blob = new Blob([buffer], {
- type: "application/octet-binary",
- });
- var reader = new FileReader();
- reader.onload = function (evt) {
- var dataurl = evt.target.result;
- callback(dataurl.substr(dataurl.indexOf(",") + 1));
- };
- reader.readAsDataURL(blob);
- }
- var pathInput = document.getElementById("path-to-read");
- addClickEnterHandler(document.getElementById("read"), pathInput, function () {
- var pathToRead = pathInput.value;
- var isFile = pathToRead.match(/\S+\.\S+$/g);
- var opts = {
- dir: getDir(),
- };
- var promise = isFile
- ? window.__TAURI__.fs.readBinaryFile(pathToRead, opts)
- : window.__TAURI__.fs.readDir(pathToRead, opts);
- promise
- .then(function (response) {
- if (isFile) {
- if (pathToRead.includes(".png") || pathToRead.includes(".jpg")) {
- arrayBufferToBase64(new Uint8Array(response), function (base64) {
- var src = "data:image/png;base64," + base64;
- registerResponse('<img src="' + src + '"></img>');
- });
- } else {
- var value = String.fromCharCode.apply(null, response);
- registerResponse(
- '<textarea id="file-response" style="height: 400px"></textarea><button id="file-save">Save</button>'
- );
- var fileInput = document.getElementById("file-response");
- fileInput.value = value;
- document
- .getElementById("file-save")
- .addEventListener("click", function () {
- window.__TAURI__.fs
- .writeFile(
- {
- file: pathToRead,
- contents: fileInput.value,
- },
- {
- dir: getDir(),
- }
- )
- .catch(registerResponse);
- });
- }
- } else {
- registerResponse(response);
- }
- })
- .catch(registerResponse);
- });
|