JS: electron use fs.readSync in render process












0















use fs.readSync() in render process,and element of buffer is always 0.
use fs.read() will get correct result.




const electron = window.require('electron');
const { remote } = electron;
const fs = remote.require('fs');

const fd = fs.openSync(localPath, 'r');
const fileStat = fs.fstatSync(fd);

const { size: fileSize } = fileStat;
const dataBuffer = Buffer.alloc(fileSize);
const readSize = 1024;
for(let i = 0; i < fileSize; i += readSize) {
fs.readSync(fd, dataBuffer, i, Math.min(fileSize - i, readSize), null);
console.log(dataBuffer);
}












share|improve this question























  • For fs you don't need to require it via electron remote (as it's default nodejs module). Write const fs = require('fs') simply. I don't know why it's not working the other way though.

    – pergy
    Jan 2 at 10:54













  • if use const fs = require('fs') directly in render process, throw error fs.openSync is not a function.Maybe it is because array is copied through remote ? Is there anyway to load a large file?

    – JiaKin
    Jan 2 at 14:29
















0















use fs.readSync() in render process,and element of buffer is always 0.
use fs.read() will get correct result.




const electron = window.require('electron');
const { remote } = electron;
const fs = remote.require('fs');

const fd = fs.openSync(localPath, 'r');
const fileStat = fs.fstatSync(fd);

const { size: fileSize } = fileStat;
const dataBuffer = Buffer.alloc(fileSize);
const readSize = 1024;
for(let i = 0; i < fileSize; i += readSize) {
fs.readSync(fd, dataBuffer, i, Math.min(fileSize - i, readSize), null);
console.log(dataBuffer);
}












share|improve this question























  • For fs you don't need to require it via electron remote (as it's default nodejs module). Write const fs = require('fs') simply. I don't know why it's not working the other way though.

    – pergy
    Jan 2 at 10:54













  • if use const fs = require('fs') directly in render process, throw error fs.openSync is not a function.Maybe it is because array is copied through remote ? Is there anyway to load a large file?

    – JiaKin
    Jan 2 at 14:29














0












0








0








use fs.readSync() in render process,and element of buffer is always 0.
use fs.read() will get correct result.




const electron = window.require('electron');
const { remote } = electron;
const fs = remote.require('fs');

const fd = fs.openSync(localPath, 'r');
const fileStat = fs.fstatSync(fd);

const { size: fileSize } = fileStat;
const dataBuffer = Buffer.alloc(fileSize);
const readSize = 1024;
for(let i = 0; i < fileSize; i += readSize) {
fs.readSync(fd, dataBuffer, i, Math.min(fileSize - i, readSize), null);
console.log(dataBuffer);
}












share|improve this question














use fs.readSync() in render process,and element of buffer is always 0.
use fs.read() will get correct result.




const electron = window.require('electron');
const { remote } = electron;
const fs = remote.require('fs');

const fd = fs.openSync(localPath, 'r');
const fileStat = fs.fstatSync(fd);

const { size: fileSize } = fileStat;
const dataBuffer = Buffer.alloc(fileSize);
const readSize = 1024;
for(let i = 0; i < fileSize; i += readSize) {
fs.readSync(fd, dataBuffer, i, Math.min(fileSize - i, readSize), null);
console.log(dataBuffer);
}








const electron = window.require('electron');
const { remote } = electron;
const fs = remote.require('fs');

const fd = fs.openSync(localPath, 'r');
const fileStat = fs.fstatSync(fd);

const { size: fileSize } = fileStat;
const dataBuffer = Buffer.alloc(fileSize);
const readSize = 1024;
for(let i = 0; i < fileSize; i += readSize) {
fs.readSync(fd, dataBuffer, i, Math.min(fileSize - i, readSize), null);
console.log(dataBuffer);
}





const electron = window.require('electron');
const { remote } = electron;
const fs = remote.require('fs');

const fd = fs.openSync(localPath, 'r');
const fileStat = fs.fstatSync(fd);

const { size: fileSize } = fileStat;
const dataBuffer = Buffer.alloc(fileSize);
const readSize = 1024;
for(let i = 0; i < fileSize; i += readSize) {
fs.readSync(fd, dataBuffer, i, Math.min(fileSize - i, readSize), null);
console.log(dataBuffer);
}






javascript electron fs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 9:26









JiaKinJiaKin

1




1













  • For fs you don't need to require it via electron remote (as it's default nodejs module). Write const fs = require('fs') simply. I don't know why it's not working the other way though.

    – pergy
    Jan 2 at 10:54













  • if use const fs = require('fs') directly in render process, throw error fs.openSync is not a function.Maybe it is because array is copied through remote ? Is there anyway to load a large file?

    – JiaKin
    Jan 2 at 14:29



















  • For fs you don't need to require it via electron remote (as it's default nodejs module). Write const fs = require('fs') simply. I don't know why it's not working the other way though.

    – pergy
    Jan 2 at 10:54













  • if use const fs = require('fs') directly in render process, throw error fs.openSync is not a function.Maybe it is because array is copied through remote ? Is there anyway to load a large file?

    – JiaKin
    Jan 2 at 14:29

















For fs you don't need to require it via electron remote (as it's default nodejs module). Write const fs = require('fs') simply. I don't know why it's not working the other way though.

– pergy
Jan 2 at 10:54







For fs you don't need to require it via electron remote (as it's default nodejs module). Write const fs = require('fs') simply. I don't know why it's not working the other way though.

– pergy
Jan 2 at 10:54















if use const fs = require('fs') directly in render process, throw error fs.openSync is not a function.Maybe it is because array is copied through remote ? Is there anyway to load a large file?

– JiaKin
Jan 2 at 14:29





if use const fs = require('fs') directly in render process, throw error fs.openSync is not a function.Maybe it is because array is copied through remote ? Is there anyway to load a large file?

– JiaKin
Jan 2 at 14:29












1 Answer
1






active

oldest

votes


















0














Return 0 probably means that the synchronized operation is failed with remote. You might capture exception in main thread.



BTW: Synchronized function call will be very slow in nodejs. That's why all these functions are named with Sync as suffix. I'd high recommend you write async code everywhere if it is possible.






share|improve this answer
























  • Maybe it is because array is copied through remote ? Is there anyway to load a large file? Transfer data from main process to render process is too slow.

    – JiaKin
    Jan 2 at 14:32











  • I don't exactly know why you want to load large local file into a browser window (render process). Here are some thoughts: (1) you can use native html5 file api to load file like any file uploader in browser (2) you can register your own secure protocol between main and render processes (like this github.com/stanleyxu2005/electron-utils/blob/master/lib/…) and then you can load contents at the fastest speed

    – stanleyxu2005
    Jan 2 at 15:40








  • 1





    just fyi, using remote is nothing more different than Transfer data from main process to render process. If you noticed some perf slowness without remote when ipc between processes, using remote will only makes slower (cause it's sync ipc underlying)

    – OJ Kwon
    Jan 2 at 22:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003899%2fjs-electron-use-fs-readsync-in-render-process%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Return 0 probably means that the synchronized operation is failed with remote. You might capture exception in main thread.



BTW: Synchronized function call will be very slow in nodejs. That's why all these functions are named with Sync as suffix. I'd high recommend you write async code everywhere if it is possible.






share|improve this answer
























  • Maybe it is because array is copied through remote ? Is there anyway to load a large file? Transfer data from main process to render process is too slow.

    – JiaKin
    Jan 2 at 14:32











  • I don't exactly know why you want to load large local file into a browser window (render process). Here are some thoughts: (1) you can use native html5 file api to load file like any file uploader in browser (2) you can register your own secure protocol between main and render processes (like this github.com/stanleyxu2005/electron-utils/blob/master/lib/…) and then you can load contents at the fastest speed

    – stanleyxu2005
    Jan 2 at 15:40








  • 1





    just fyi, using remote is nothing more different than Transfer data from main process to render process. If you noticed some perf slowness without remote when ipc between processes, using remote will only makes slower (cause it's sync ipc underlying)

    – OJ Kwon
    Jan 2 at 22:41
















0














Return 0 probably means that the synchronized operation is failed with remote. You might capture exception in main thread.



BTW: Synchronized function call will be very slow in nodejs. That's why all these functions are named with Sync as suffix. I'd high recommend you write async code everywhere if it is possible.






share|improve this answer
























  • Maybe it is because array is copied through remote ? Is there anyway to load a large file? Transfer data from main process to render process is too slow.

    – JiaKin
    Jan 2 at 14:32











  • I don't exactly know why you want to load large local file into a browser window (render process). Here are some thoughts: (1) you can use native html5 file api to load file like any file uploader in browser (2) you can register your own secure protocol between main and render processes (like this github.com/stanleyxu2005/electron-utils/blob/master/lib/…) and then you can load contents at the fastest speed

    – stanleyxu2005
    Jan 2 at 15:40








  • 1





    just fyi, using remote is nothing more different than Transfer data from main process to render process. If you noticed some perf slowness without remote when ipc between processes, using remote will only makes slower (cause it's sync ipc underlying)

    – OJ Kwon
    Jan 2 at 22:41














0












0








0







Return 0 probably means that the synchronized operation is failed with remote. You might capture exception in main thread.



BTW: Synchronized function call will be very slow in nodejs. That's why all these functions are named with Sync as suffix. I'd high recommend you write async code everywhere if it is possible.






share|improve this answer













Return 0 probably means that the synchronized operation is failed with remote. You might capture exception in main thread.



BTW: Synchronized function call will be very slow in nodejs. That's why all these functions are named with Sync as suffix. I'd high recommend you write async code everywhere if it is possible.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 9:44









stanleyxu2005stanleyxu2005

5,201104678




5,201104678













  • Maybe it is because array is copied through remote ? Is there anyway to load a large file? Transfer data from main process to render process is too slow.

    – JiaKin
    Jan 2 at 14:32











  • I don't exactly know why you want to load large local file into a browser window (render process). Here are some thoughts: (1) you can use native html5 file api to load file like any file uploader in browser (2) you can register your own secure protocol between main and render processes (like this github.com/stanleyxu2005/electron-utils/blob/master/lib/…) and then you can load contents at the fastest speed

    – stanleyxu2005
    Jan 2 at 15:40








  • 1





    just fyi, using remote is nothing more different than Transfer data from main process to render process. If you noticed some perf slowness without remote when ipc between processes, using remote will only makes slower (cause it's sync ipc underlying)

    – OJ Kwon
    Jan 2 at 22:41



















  • Maybe it is because array is copied through remote ? Is there anyway to load a large file? Transfer data from main process to render process is too slow.

    – JiaKin
    Jan 2 at 14:32











  • I don't exactly know why you want to load large local file into a browser window (render process). Here are some thoughts: (1) you can use native html5 file api to load file like any file uploader in browser (2) you can register your own secure protocol between main and render processes (like this github.com/stanleyxu2005/electron-utils/blob/master/lib/…) and then you can load contents at the fastest speed

    – stanleyxu2005
    Jan 2 at 15:40








  • 1





    just fyi, using remote is nothing more different than Transfer data from main process to render process. If you noticed some perf slowness without remote when ipc between processes, using remote will only makes slower (cause it's sync ipc underlying)

    – OJ Kwon
    Jan 2 at 22:41

















Maybe it is because array is copied through remote ? Is there anyway to load a large file? Transfer data from main process to render process is too slow.

– JiaKin
Jan 2 at 14:32





Maybe it is because array is copied through remote ? Is there anyway to load a large file? Transfer data from main process to render process is too slow.

– JiaKin
Jan 2 at 14:32













I don't exactly know why you want to load large local file into a browser window (render process). Here are some thoughts: (1) you can use native html5 file api to load file like any file uploader in browser (2) you can register your own secure protocol between main and render processes (like this github.com/stanleyxu2005/electron-utils/blob/master/lib/…) and then you can load contents at the fastest speed

– stanleyxu2005
Jan 2 at 15:40







I don't exactly know why you want to load large local file into a browser window (render process). Here are some thoughts: (1) you can use native html5 file api to load file like any file uploader in browser (2) you can register your own secure protocol between main and render processes (like this github.com/stanleyxu2005/electron-utils/blob/master/lib/…) and then you can load contents at the fastest speed

– stanleyxu2005
Jan 2 at 15:40






1




1





just fyi, using remote is nothing more different than Transfer data from main process to render process. If you noticed some perf slowness without remote when ipc between processes, using remote will only makes slower (cause it's sync ipc underlying)

– OJ Kwon
Jan 2 at 22:41





just fyi, using remote is nothing more different than Transfer data from main process to render process. If you noticed some perf slowness without remote when ipc between processes, using remote will only makes slower (cause it's sync ipc underlying)

– OJ Kwon
Jan 2 at 22:41




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003899%2fjs-electron-use-fs-readsync-in-render-process%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas