Web User
Web User

Reputation: 7736

Determine file type using Electron dialog

I used HTML5 File API to drag a file into an Electron application and obtained file details (name, mime-type, size, etc.). How can I achieve the same when selecting a file via Electron's dialog module? Below is the code (renderer process) that leverages HTML5's File API:

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

// Using jQuery ($)
var holder = $('#holder');
holder.on('drag dragstart dragend dragover dragenter dragleave drop', function(evt) {
    evt.preventDefault();
    evt.stopPropagation();
})
.on('drop', function(evt) {
    let file = evt.originalEvent.dataTransfer.files[0];
    console.log(file.name);
    console.log(file.type);
    console.log(file.size);
})
.on('click', function(evt) {
    dialog.showOpenDialog({
        properties: [ 'openFile' ]
    }, function(file) {
        console.log(file); // just displays local, full path
        // code to get name, type, size... how do I?
    });
});

Upvotes: 0

Views: 1138

Answers (1)

Riad Loukili
Riad Loukili

Reputation: 119

Check this library: mmmagic, it will do just what you want.

Upvotes: 1

Related Questions