OnGetFilePath event not firing

536 views Asked by At

After a user has scanned a document - I would like to allow them to select a location to save the file, save the file, and finally return the path to the file they have just saved.

I am trying to use the "OnGetFilePath" event but it's not working.

JS Code is here:

var DWObject;

Dynamsoft.WebTwainEnv.AutoLoad = false;
Dynamsoft.WebTwainEnv.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady);


function LoadEnv() {
    Dynamsoft.WebTwainEnv.Load();
}

function Dynamsoft_OnReady() {
    DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');

    if (DWObject) {

        DWObject.IfShowFileDialog = true;
        DWObject.RegisterEvent('OnGetFilePath', OnGetFilePath); 

        DWObject.SelectSource(function () {
            DWObject.OpenSource();
            DWObject.IfDisableSourceAfterAcquire = true;
            DWObject.AcquireImage(OnAcquireImageSuccess, OnAcquireImageFailure);
        }, function () {
            console.log('SelectSource failed!');
        });
    }
}

function OnAcquireImageSuccess() {
    console.log('Successfully aquired image');
    SavePDF();
    DWObject.CloseSource();
}

//File saved to disk successfully 
function SavePDF() {
    DWObject.SaveAsPDF('file.pdf');
}

//Not Fired
function OnGetFilePath(bSave, filesCount, index, path, filename) {
    console.log("File Path!");
} 
1

There are 1 answers

5
Hassan Voyeau On BEST ANSWER

This is what I did and I think it will help you.

  • I downloaded the sample code - https://demo.dynamsoft.com/DWT/online_demo_scan.aspx
  • I added a button next to the Scan button on online_demo_scan.html

    <input id="btnTest" value="TEST" type="button" onclick="DWObject.ShowFileDialog(false, 'JPG,PNG,TIF', 0, '', '', true, true, 0);"/>

  • I added an alert to Dynamsoft_OnGetFilePath in online_demo_operation.js

    function Dynamsoft_OnGetFilePath(bSave, count, index, path, name) { alert('OnGetFilePath event fired!'); }

And the event does fire when I click the button.

According to the documentation

This event is triggered when 1. the ShowFileDialog method has finished; 2. the method LoadImageEx has finished with IfShowFileDialog set to true.

EDIT : Some additional code to show how to save the file once the event gets fired.

function Dynamsoft_OnGetFilePath(bSave, count, index, path, name) {
    var file_path = path + "\\" + name + ".pdf";
    DWObject.IfShowFileDialog = false;
    DWObject.SaveAsPDF(file_path);
    alert(file_path);
}