新文件卡住 [英] New File stucks

查看:67
本文介绍了新文件卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java语言的新手,正尝试在本地txt文件中读写,因此我像在网络上那样进行操作,但由于某种原因,它卡在了新的File实例中:

I am new in javascript and was trying to read and write in local txt file so I did as I found in the web but for some reason it stucks in new File instantiation:

<script type="text/javascript">
var txtFile = "d:/test.txt"
alert('point 1');
var file = new File(txtFile);
alert('point 2');
</script>

它仅打印"point 1"字符串.谢谢.

It prints only 'point 1' string. Thanks.

推荐答案

new File() 构造函数至少需要两个参数;第一个参数是包含StringArrayBufferViewArrayBufferBlob或另一个File对象的数组;第二个文件名.您无法使用javascript指定文件的下载目录,但可以在浏览器设置或首选项中设置下载目录.您还可以利用a元素上的download属性在Save File对话框中设置文件名.

new File() constructor requires at least two parameters; first parameter an array containing String, ArrayBufferView, ArrayBuffer Blob or another File object; the second the file name. You cannot specify a download directory for files using javascript, but you can set a download directory at browser settings or preferences. You can also utilize download attribute at a element to set file name at Save File dialog.

我是javascript的新手,正尝试以本地txt读写 文件

I am new in javascript and was trying to read and write in local txt file

您可以使用<input type="file">元素检索test.txt,并在<textarea>上编辑.txt文件,然后再使用相同的文件名进行保存.另请参见编辑,保存,自修改HTML文档;格式生成的HTML,JavaScript 如何编写使用JavaScript在文件(用户目录)中存储?

You could use <input type="file"> element to retrieve test.txt, and edit .txt file at <textarea> before re-saving with same file name. See also Edit, save, self-modifying HTML document; format generated HTML, JavaScript , How to Write in file (user directory) using JavaScript?

<script type="text/javascript">
  var txtFile = "test.txt";
  var text = "abc";
  var file = new File([text], txtFile);
  console.log(file);
  var a = document.createElement("a");
  a.download = file.name;
  a.href = URL.createObjectURL(file);
  document.body.appendChild(a);
  a.innerHTML = file.name;
  a.onclick = function() {
    this.parentElement.removeChild(this)
  }
</script>

这篇关于新文件卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆