JS script example for LocalFileOpen/LocalFileWriteString/LocalFileOpen

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

JS script example for LocalFileOpen/LocalFileWriteString/LocalFileOpen

ソリューションへジャンプ
1,227件の閲覧回数
Fred_Liu
Contributor II

Hello

  I tried to use FM3.1 JS(ActiveX) to write or read FM variable to my local folder but fail. Can I print out the "FMSTR_PROJECT_PATH"? Any related example for local file handle?

 

Thanks a lot

Fred

 

0 件の賞賛
返信
1 解決策
1,224件の閲覧回数
MichalH
NXP Apps Support
NXP Apps Support

Hello, 

The FMSTR_PROJECT_PATH represents path to a saved FreeMASTER project file (.pmp/.pmpx file).

Typically you will have your HTML page stored in the same directory as project file or (even better) in a subdirectory of a directory where the project file is stored. This will enable you to pack the HTML and all other resource files (pictures, script files, etc) to the .pmp file when saving (see User Guide section 5.3).

See a minimalistic test page code below. Save it as "control.htm" together with the project file and specify its name as the Control page URL in project options.

 

control_page_html.png

 

<html><head></head>
<body>
<OBJECT id="pcm" height="0" width="0" classid="clsid:48A185F1-FFDB-11D3-80E3-00C04F176153">
</OBJECT>

<script
 language="JScript">

function TestWrite() {
    var handle = pcm.LocalFileOpen("FMSTR_PROJECT_PATH/" + file_name.value, "a");
    if(handle) {
        var characters_written = pcm.LocalFileWriteString(handle, to_write.value + "\r\n");
        pcm.LocalFileClose(handle);

        alert(characters_written + " characters written to '" + file_name.value + "'");
    }
    else {
        alert("Cannot open '" + file_name.value + "' "  + pcm.LastRetMsg);
    }
}

</script>

FreeMASTER Write test <br>

filename <input id="file_name" type="text" value="test.txt"> <br>
text <input id="to_write" type="text" value="string to write"> <br>
<input id="do_write" type="button" onclick="TestWrite()" value="Write!">

</body></html>

Regards,
Michal

元の投稿で解決策を見る

0 件の賞賛
返信
1 返信
1,225件の閲覧回数
MichalH
NXP Apps Support
NXP Apps Support

Hello, 

The FMSTR_PROJECT_PATH represents path to a saved FreeMASTER project file (.pmp/.pmpx file).

Typically you will have your HTML page stored in the same directory as project file or (even better) in a subdirectory of a directory where the project file is stored. This will enable you to pack the HTML and all other resource files (pictures, script files, etc) to the .pmp file when saving (see User Guide section 5.3).

See a minimalistic test page code below. Save it as "control.htm" together with the project file and specify its name as the Control page URL in project options.

 

control_page_html.png

 

<html><head></head>
<body>
<OBJECT id="pcm" height="0" width="0" classid="clsid:48A185F1-FFDB-11D3-80E3-00C04F176153">
</OBJECT>

<script
 language="JScript">

function TestWrite() {
    var handle = pcm.LocalFileOpen("FMSTR_PROJECT_PATH/" + file_name.value, "a");
    if(handle) {
        var characters_written = pcm.LocalFileWriteString(handle, to_write.value + "\r\n");
        pcm.LocalFileClose(handle);

        alert(characters_written + " characters written to '" + file_name.value + "'");
    }
    else {
        alert("Cannot open '" + file_name.value + "' "  + pcm.LastRetMsg);
    }
}

</script>

FreeMASTER Write test <br>

filename <input id="file_name" type="text" value="test.txt"> <br>
text <input id="to_write" type="text" value="string to write"> <br>
<input id="do_write" type="button" onclick="TestWrite()" value="Write!">

</body></html>

Regards,
Michal
0 件の賞賛
返信