JS script example for LocalFileOpen/LocalFileWriteString/LocalFileOpen

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

JS script example for LocalFileOpen/LocalFileWriteString/LocalFileOpen

跳至解决方案
1,140 次查看
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,137 次查看
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,138 次查看
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 项奖励
回复