tattyan3
@tattyan3 (tatsuji shimo)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

出来れば、良いのにね。読込。コピペなら出来るのに、あっても出来ないし。

HTML


	<input type="text" name="text1" value="Please write the directory." id="my_text1" onchange="text_Changed(value)"><br>
	<input type="file" onchange="file_Changed(value)"><br>
	<!--<span id="my_inna_text">てきすと<span><br>-->
	<textarea name="txt" cols="40" rows="5" id="mytextarea"></textarea><br>
	<hr>
	<b>form</b><br>
	<form name="my_form_0">
		<input type="file" id="selfile" accept=".txt" onchange="text_read(value)">
	    <span id="errmessage">errmessage</span><br>
	   <input type="radio" checked onchange="radio_glope1(1,checked)">Radio1 <input type="radio" onchange="radio_glope1(2,checked)">Radio2<br>
		<input type="file" onchange="fileChanged(value)"><br>
		<textarea name="txt" cols="40" rows="5" class="mytextr"></textarea>
		<input type="button" value="change" onclick="winopen1()">
		<div id="my_div1"><iframe src="./text/me_text0.html" id="me_text" value=this></iframe></div>
	</form>
	<hr>	
	<span id="my_in_a_text" onclick="it_test2('test')">てきすと</span><br>
	<!--<span id="my_inna_text" onclick="alert('it chenge!!');">てきすと<span><br>-->
    <input type="button" value="テキストリード" onclick="read()"><br>
	<p id="mfl">x</p>
    <p id="f_name">filename</p><br>
    <div class="image-list">image-list</div>

スクリーンショット 2024-03-18 190030.png
スクリーンショット 2024-03-18 190109.png
スクリーンショット 2024-03-18 190148.png

function winopen1() {

	alert("in_iku")
	document.querySelector('#my_div1').innerHTML = '<iframe src="./text/steping.txt" id="me_text" value=this></iframe>';

	document.forms[0].elements[4].value = document.getElementById("me_text").value;

}

//              form end
// no coll
function read() {
	alert("my_mese"+fail_name);
    let redotext = document.querySelector('.mytextr').value;
    // document.querySelector('#mfl').innerHTML = redotext.replace(/\n/g, '<br>');
    let readlin = [];
    readlin[0] = '';
    let ii = 0;
	
    for (let i = 0; i < redotext.length; i++) {
        if (!redotext[i].includes('\n')) {
            readlin[ii] += redotext[i];
        } else {
            ii++;
            readlin[ii] = '';
        }
    }
    document.querySelector('#mfl').innerHTML = readlin;
}

妄想論を書きました。me.txt から、text value に読み込めれば良いだけだが、知らんし。

0

<input type="file" id="fileInput" onchange="readFile(this)">
<div id="fileContent"></div>

<script>

  function readFile(input) {
    let file = input.files[0];
    let reader = new FileReader();

    reader.onload = function() {
      document.getElementById('fileContent').textContent = reader.result;
    };

    reader.readAsText(file);
  }

</script>

AIのPhindさんが書いたサンプルです。

1Like

こんな感じでしょうか?

const openFocusTimeout = 5000;

// example start;
const file = await selectFile();
const encodeName = (encode.value ?? '') === '' ? undefined : encode.value;
const text = await readAsText(file, encodeName);
console.log(text);
// example end;

function selectFile(options = {}) {
  let removeElement;
  const deferred = (() => {
    let resolve, reject;
    const promise = new Promise((res,rej) => [resolve,reject] = [res,rej]);
    return {resolve, reject, promise};
  })();
  try {
    const input = document.createElement('input');
    input.style.display = 'none';
    input.type = 'file';
    input.value = '';
    const isMultiple = options?.multiple ?? false;
    if (isMultiple) {
      input.multiple = true;
    }
    if (options?.accept) {
      input.accept = options.accept;
    }
    if (options?.capture) {
      input.capture = options.capture;
    }
    if (options?.directory) {
      input.webkitdirectory = options.directory;
    }
    document.body.appendChild(input);
    // 選択ダイアログを閉じたタイミングで focus イベントが、
    // 選択していればその後 change イベントが発生する。
    // change イベントが openFocusTimeout ms 後に 発生しなければ キャンセルと解釈する
    input.addEventListener("change", change, { once: true });
    window.addEventListener("focus", focus, { once: true });
    function change() {
      deferred.resolve(
        (isMultiple ? input.files : input.files?.[0]) ?? null
      );
    }
    async function focus() {
      await timeout(openFocusTimeout);
      deferred.resolve(null);
    }
    removeElement = () => {
      input.remove();
      input.removeEventListener("change", change);
      window.removeEventListener("focus", focus);
    };
    input.click();
  } catch(e) {
    deferred.reject(e);
  }
  return deferred.promise;
}
function timeout(milliseconds = undefined)
{
  return new Promise(resolve => {
    setTimeout(resolve, milliseconds);
  })
}
function readAsText(file, encode = undefined) {
  console.assert(file instanceof File || file instanceof Blob);
  const deferred = (() => {
    let resolve,reject;
    const promise = new Promise((res, rej) => [resolve, reject] = [res, rej]);
    return {promise, resolve, reject};
  })();
  const reader = new FileReader();
  reader.addEventListener("load", (e) => {
    const result = e.target?.result;
    deferred.resolve(result);
  });
  reader.addEventListener("error", (error) => {
    deferred.reject(error);
  });
  if (encode) reader.readAsText(file, encode);
  else reader.readAsText(file);
  return deferred.promise;
}

1Like

Your answer might help someone💌