source: branches/testa/js/remote.js @ 24

Last change on this file since 24 was 23, checked in by anonymous, 6 years ago

init

File size: 4.4 KB
RevLine 
[23]1function upload_to_host(part) {
2    if( Globalx[part].key_indicate_file === true ){
3        let textarea = $( Globalx[part].textarea_sel )
4        upload_content(textarea.val(), Globalx[part].key, Globalx.num)
5    }
6}
7
8function upload_content(content, path, num) {
9    let form = document.createElement('form');
10    let req_content = document.createElement('input');
11    let req_path = document.createElement('input');
12    let req_cmd = document.createElement('input');
13    let req_num = document.createElement('input');
14
15    document.body.appendChild(form);
16    req_content.type = 'hidden';
17    req_content.name = 'mytext';
18    req_content.value = content;
19   
20    req_path.type = 'hidden';
21    req_path.name = 'path';
22    req_path.value = path;
23   
24    req_cmd.type = 'hidden';
25    req_cmd.name = 'cmd';
26    req_cmd.value = 'upload_content';
27   
28    req_num.type = 'hidden';
29    req_num.name = 'num';
30    req_num.value = num + '';
31   
32    form.appendChild(req_content);
33    form.appendChild(req_path);
34    form.appendChild(req_cmd);
35    form.appendChild(req_num);
36   
37    // FormDataオブジェクトを作成する
38    var form_data = new FormData(form);
39
40    fetch('php/content.php', {
41        method: 'POST',
42        mode: 'same-origin', /* 'no-cors' 'cors' 'same-origin' */
43        body: form_data
44    })
45    .then((response) => response.text())
46    .then((data) => console.log(data))
47    .catch((error) => console.log(error));
48}
49
50function make_url_params( arrayx ){
51      let params = new URLSearchParams();
52      arrayx.reduce(function(acc, element, index, array) {
53          acc.append(element[0], element[1]);
54          return acc;
55      }, params)
56      return params;
57}
58
59function get_filelist_from_remote() {
60    let param_array = [['cmd', 'get_filelist'], ['num', Globalx.num ]];
61    let params = make_url_params( param_array );
62    let filename = "php/content.php"
63    fetch(`${filename}?${params}`)
64    .then((response) => response.text())
65    .then((str) => save_filelist_to_local_storage(str, num))
66    .catch((error) => console.log(error));
67}
68
69function update_filelist() {
70    let param_array = [['cmd', 'update_filelist'], ['num', Globalx.num ]];
71    let params = make_url_params( param_array );
72    let filename = "php/content.php"
73    fetch(`${filename}?${params}`)
74    .then((response) => response.text())
75    .then((text) => save_filelist_to_local_storage(text, num))
76    .catch((error) => console.log(error));
77}
78
79function get_content(path , func) {
80    console.log( "path:" + path )
81    let param_array = [['cmd', 'get_content'], ['num', Globalx.num ], ['path', path]];
82    let params = make_url_params( param_array );
83    let filename = "php/content.php"
84    fetch(`${filename}?${params}`)
85    .then((response) => response.text())
86    .then((text) => {func(text)})
87    .catch((error) => console.log(error));
88}
89
90//
91//
92function restore_filelist(num, key){
93    const data = get_filelist(num);
94    if( data !== null ){
95//      display_filelist( data, '#filelist' );
96//      console.log( data['/'] );
97//      console.log( data['/rtk'] );
98//      console.log( data['/rtk/fmp_kernel'] );
99//      console.log( data['/rtk/fmp_kernel/data'] );
100//      console.log( data['/rtk/fmp_kernel/data/fmp-kernel.md'] );
101        console.log( data[key] );
102    }
103    else{
104        console.log("null returned!");
105    }
106}
107function handleDownload(e) {
108    let dl = document.getElementById("down-download")
109    if( Globalx[ e.data.part ].key_indicate_file === true ){
110        console.log("handleDownload-file")
111        console.log(Globalx[ e.data.part ])
112
113        get_content( Globalx[ e.data.part ].key , function(content) {
114            let blob = new Blob([ content ], { "type" : "text/plain" });
115
116            if (window.navigator.msSaveBlob) {
117                window.navigator.msSaveBlob(blob, Globalx[ e.data.part ].item_name);
118                console.log("handleDownload=" + Globalx[ e.data.part ].item_name)
119               
120                // msSaveOrOpenBlobの場合はファイルを保存せずに開ける
121                window.navigator.msSaveOrOpenBlob(blob, Globalx[ e.data.part ].item_name);
122            } else {
123                console.log( "handleDownLoad-1-2" )
124                console.log("handleDownload=" + Globalx[ e.data.part ].item_name)
125                dl.download = Globalx[ e.data.part ].item_name
126                dl.href = window.URL.createObjectURL(blob);
127            }
128        })
129    }
130    else{
131        e.preventDefault()
132        console.log("handleDownload-Not file")
133        dl.href = ""
134        dl.download = ""
135    }
136}
137
138function download_cmd(part) {
139    if( Globalx[part].key_indicate_file === true ){
140        console.log("download_cmd filename=" + Globalx[part].filename)
141
142        let dl = $( Globalx[part].download_sel )
143
144        console.log( dl.attr('download') )
145        dl.trigger('click')
146    }
147}
148
Note: See TracBrowser for help on using the repository browser.