最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can you saveload a file via Javascript? - Stack Overflow

programmeradmin15浏览0评论

I want to create a very simple Javascript game using HTML5 (Canvas). But is it possible to save a simple .txt file and load a simple .txt file. I just need to store like the some simple integers. But I just want to know if javascript is allowed to save and load an external file?

Canvas

I want to create a very simple Javascript game using HTML5 (Canvas). But is it possible to save a simple .txt file and load a simple .txt file. I just need to store like the some simple integers. But I just want to know if javascript is allowed to save and load an external file?

Canvas

Share Improve this question asked Jul 30, 2013 at 20:04 CanvasCanvas 5,8979 gold badges59 silver badges102 bronze badges 5
  • 1 You definitely can't write to the local disk, but most browsers do have some sort of local storage. – Mike Christensen Commented Jul 30, 2013 at 20:07
  • if security doesn't matter at all, you can try document.cookie, which is really easy to use – user428517 Commented Jul 30, 2013 at 20:12
  • Check out this link; stackoverflow.com/questions/2897619/… – JNL Commented Jul 30, 2013 at 20:13
  • @MikeChristensen You can force a download from RAM nowadays (see question linked by JNL). – bfavaretto Commented Jul 30, 2013 at 20:16
  • HTML5 introduces file system support, and Chrome already implements it: html5rocks.com/en/tutorials/file/filesystem – MatteoSp Commented Jul 30, 2013 at 20:18
Add a comment  | 

4 Answers 4

Reset to default 4

Since html5 you can use the LocalStorage API. Nowadays almost all browsers support it:

// Check if it is supported in your browser
function supports_html5_storage()
{
      try
      {
        return 'localStorage' in window && window['localStorage'] !== null;
      }
      catch (e)
      {
        return false;
      }
}

//make use of it:
if( supports_html5_storage() == true )
{
   localStorage.setItem("myItem", "myData");
   var myDataString = localStorage.getItem("myItem");
   alert(myDataString);
}

On Chrome, you can rely on the FileSystem API (for an intro take a look here). Probably other browsers will soon add support to it.

But, if your need is just "to store like the some simple integers" I would consider local storage.

In short, no. According David Flanagan's "JavaScript: The Definitive Guide":

Input and output (as well as more sophisticated features, such as networking, storage, and graphics) are the responsibility of the 'host environment' within which JavaScript is embedded.

The bigger question is why. Think about how dangerous it would be if JavaScript could write files to your hard drive. What if any website you visited could access your local file system?

You can't access the local file system directly with javascript, but it is possible when you let the user interact (for example by letting the user select a file to upload). See http://www.html5rocks.com/en/tutorials/file/dndfiles/

Another possibility is local storage. See http://davidwalsh.name/html5-storage, http://www.w3.org/TR/webstorage/

发布评论

评论列表(0)

  1. 暂无评论