使用Javascript创建文件

使用Javascript创建文件,javascript,Javascript,是否可以使用javascript在localhost上创建文件?不在网页中。如果您使用的是Windows脚本主机,那么是的,您可以通过ActiveX进行访问,但我认为您没有这样做。但是,您可以通过AJAX将数据发送回Web服务器,并让它为您存储数据。不在网页中。如果您使用的是Windows脚本主机,那么是的,您可以通过ActiveX进行访问,但我认为您没有这样做。但是,您可以通过AJAX将数据发送回Web服务器,并让它为您存储数据。不,这将是一个安全问题 您可以通过插件创建文件,请参见否,这将是

是否可以使用javascript在localhost上创建文件?

不在网页中。如果您使用的是Windows脚本主机,那么是的,您可以通过ActiveX进行访问,但我认为您没有这样做。但是,您可以通过AJAX将数据发送回Web服务器,并让它为您存储数据。

不在网页中。如果您使用的是Windows脚本主机,那么是的,您可以通过ActiveX进行访问,但我认为您没有这样做。但是,您可以通过AJAX将数据发送回Web服务器,并让它为您存储数据。

不,这将是一个安全问题


您可以通过插件创建文件,请参见

否,这将是一个安全问题

您可以通过插件创建文件,请参见

您可以创建以在本地计算机上存储数据,这几乎是在本地计算机上创建文件的唯一方法。

您可以创建以在本地计算机上存储数据,这几乎是在本地计算机上创建文件的唯一方法

<html>
<head>
<title>Create File</title>

<! This function will create a file named 'newfile' on the same directory as the HTML unless path is given>

<script language="javascript">
function openFile()
{ var filePath = 'c:/filename.txt';
var fileSysObj = new ActiveXObject('Scripting.FileSystemObject');

fileSysObj.CreateTextFile(filePath);
}
</script>
</head>

<body>
这将在您的c:\驱动器上创建一个名为filename.txt的文件。 您必须接受ActiveX控件或禁用创建文件的提示

<button type=submit name=button onClick="openFile();">create file</button>
</body>
</html>
这将在您的c:\驱动器上创建一个名为filename.txt的文件。 您必须接受ActiveX控件或禁用创建文件的提示

<button type=submit name=button onClick="openFile();">create file</button>
</body>
</html>

我想你已经准备好了文件的内容。然后,您可以按如下方式提示“另存为”对话框:

var exportText; // this variable needs to contain your content
var targetFilename = "myfilename.ext"

function presentExportFile() {
  var download = document.createElement('a');
  // you need to change the contenttype to your needs in the next line.
  download.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(exportText));
  download.setAttribute('download', targetFilename);

  download.style.display = 'none';
  document.body.appendChild(download);

  download.click();

  document.body.removeChild(download);
}

2017年补遗:自从我写了这篇文章,我有一个异国浏览器xombrero拒绝了它。所以,我不能肯定这就是方法。

我假设您已经准备好了文件的内容。然后,您可以按如下方式提示“另存为”对话框:

var exportText; // this variable needs to contain your content
var targetFilename = "myfilename.ext"

function presentExportFile() {
  var download = document.createElement('a');
  // you need to change the contenttype to your needs in the next line.
  download.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(exportText));
  download.setAttribute('download', targetFilename);

  download.style.display = 'none';
  document.body.appendChild(download);

  download.click();

  document.body.removeChild(download);
}

2017年补遗:自从我写了这篇文章,我有一个异国浏览器xombrero拒绝了它。所以,我不能肯定这是一种方式。

这个问题已经提出4年了,而且已经发生了很多变化。如果您现在对此感到好奇,请查看FileWriter、localstorage、indexeddb、requestFileSystem、LocalFileSystem和其他选项。到2013年年中仍然不完全可行,但我们正在实现这一目标。这个问题已经提出了4年,而且已经发生了很大变化。如果您现在对此感到好奇,请查看FileWriter、localstorage、indexeddb、requestFileSystem、LocalFileSystem和其他选项。到2013年年中还不能完全实现,但我们正在实现。链接断了,jetlink断了,jet断了