【问题标题】:how do you write to a file in clojurescript?您如何在 clojurescript 中写入文件?
【发布时间】:2015-03-31 01:38:44
【问题描述】:

我正在运行 clojurescript (cljs) 浏览器 repl,我希望能够将一些文本写入本地文件。

我尝试了以下方法:

(spit "abc.txt" "hello")

但这会返回:

#<TypeError: Cannot read property 'call' of undefined>
TypeError: Cannot read property 'call' of undefined

在 clojure repl 下,这将在我的项目的根目录中创建文件“abc.txt”。

我意识到 'spit 是一个 clojure 函数,但我想知道在 cljs 中是否还有一些简单的方法可以做到这一点?

或者这是一个严格的 JavaScript 问题,与 clojurescript 无关?

【问题讨论】:

标签: clojure clojurescript


【解决方案1】:

从浏览器我认为你不能(比如在 JS 中),因为 安全性

从 nodejs 检查 nodejs 文档 :)

现在,如果你混入一点 flash (yuk :|...),就会有一个 JS 库(你可以在 cljs 中使用)。

https://github.com/dcneiner/Downloadify

【讨论】:

    【解决方案2】:

    我将此作为示例解决方案发布,我并不是说您应该这样做。然而,我确实经历了一些“陷阱”,所以我 认为值得记录。

    感谢您的回答。在做我知道我必须做的事情之前,我只是想找出是否有更简单的方法。我决定使用服务器端写入。

    我正在使用苹果酒、栗子和与 chrome 客户端连接的 brepl 服务器。由于我不熟悉环形服务器、compojure 和嵌入式码头服务器,我不知道如何向 brepl 服务器(端口 10555)添加 Web 服务。相反,我向我的本地 apache 服务器添加了一个。

    因为请求是从码头服务器(而不是从浏览器)进入 apache,所以我遇到了“跨源资源共享”问题,即消息:

    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    

    每当我向我的服务 URL 提交请求时。

    我能够通过将以下内容添加到我的 http 标头来解决此问题:

         -access_control_allow_credentials => 'true',
         -access_control_allow_origin => 'http://localhost:10555',
    

    这是唯一的问题。除此之外,它是一个标准的 cgi 解决方案(是的,老派——我来自 perl 背景)。是的,我知道如果我能在 clojure(或 Rails)中完成它会是最好的。

    我的 cgi 最终看起来像这样:

    #!/usr/bin/perl
    use CGI qw(:standard);
    print header(
                 -type => 'text/html',
                 -access_control_allow_credentials => 'true',
                 -access_control_allow_origin => 'http://localhost:10555',
                       );
    
    my $q = CGI->new();
    my $src = $q->param('src');
    
    # write to file
     open(my $fh, ">>", "vt-src-out.txt")
        or die "cannot open < vt-src-out.txt: $!";
    
    print $fh "$src\n"; 
    

    我使用 ajax.core 作为我的客户端 api:

    :require [ajax.core :refer [GET POST]]
    

    并像这样调用服务:

    (GET "http://localhost/cgi-bin/cljs-write-src.cgi" {:params {:src "(defn foo [])(+ 1 1)"}})
    

    【讨论】:

      【解决方案3】:

      node.js 示例。

      比编写 cgi 更轻松。 node.js 的新手,所以只是一个初学者。没有转换成 cljs。

      // to run:
      // node write_file.js
      // to call from cmd line:
      // curl localhost:9090?fn=test.txt\&msg=hello%20world
      
      var http = require('http');
      
      var fs = require('fs');
      var url = require('url');
      
      var server = http.createServer(function(req, res) {
      var fn = url.parse(req.url, true).query['fn'];
      var msg = url.parse(req.url, true).query['msg'];
      
      fs.writeFile(process.env.HOME + "/vtstuff/tmp/" + fn, msg + "\n",               
        function(err) {
      
        if(err) {
            return console.log(err);
        }
      
        console.log("The file was saved!");
        console.log("fn=" + fn);
        console.log("msg=" + msg);
        process.argv.forEach(function (val, index, array) {
              console.log(index + ': ' + val);
        });
        }); 
        res.writeHead(200,{"Content-Type": "text/plain"});
        res.end("wrote file\n");
      });
      
      server.listen(9090);
      

      【讨论】:

        猜你喜欢
        • 2012-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多