| 1 | function 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 | |
|---|
| 8 | function 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 | |
|---|
| 50 | function 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 | |
|---|
| 59 | function 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 | |
|---|
| 69 | function 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 | |
|---|
| 79 | function get_content(path , func) { |
|---|
| 80 | let param_array = [['cmd', 'get_content'], ['num', Globalx.num ], ['path', path]]; |
|---|
| 81 | let params = make_url_params( param_array ); |
|---|
| 82 | let filename = "php/content.php" |
|---|
| 83 | fetch(`${filename}?${params}`) |
|---|
| 84 | .then((response) => response.text()) |
|---|
| 85 | .then((text) => {func(text)}) |
|---|
| 86 | .catch((error) => console.log(error)); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | function handleDownload(e) { |
|---|
| 90 | let dl = document.getElementById("down-download") |
|---|
| 91 | if( Globalx[ e.data.part ].key_indicate_file === true ){ |
|---|
| 92 | |
|---|
| 93 | get_content( Globalx[ e.data.part ].key , function(content) { |
|---|
| 94 | let blob = new Blob([ content ], { "type" : "text/plain" }); |
|---|
| 95 | |
|---|
| 96 | if (window.navigator.msSaveBlob) { |
|---|
| 97 | window.navigator.msSaveBlob(blob, Globalx[ e.data.part ].item_name); |
|---|
| 98 | |
|---|
| 99 | // msSaveOrOpenBlobの場合はファイルを保存せずに開ける |
|---|
| 100 | window.navigator.msSaveOrOpenBlob(blob, Globalx[ e.data.part ].item_name); |
|---|
| 101 | } else { |
|---|
| 102 | dl.download = Globalx[ e.data.part ].item_name |
|---|
| 103 | dl.href = window.URL.createObjectURL(blob); |
|---|
| 104 | } |
|---|
| 105 | }) |
|---|
| 106 | } |
|---|
| 107 | else{ |
|---|
| 108 | e.preventDefault() |
|---|
| 109 | dl.href = "" |
|---|
| 110 | dl.download = "" |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | function download_cmd(part) { |
|---|
| 115 | if( Globalx[part].key_indicate_file === true ){ |
|---|
| 116 | let dl = $( Globalx[part].download_sel ) |
|---|
| 117 | dl.trigger('click') |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|