简体   繁体   English

node.js将字符串转换为文件以上传到Web服务器

[英]node.js turning string into file to be uploaded to webserver

Let's say I have a string that is dynamically created using defined variables. 假设我有一个使用定义的变量动态创建的字符串。 For example: 例如:

<html>
   <head>
   .... blah blah blah ....
   </head>

   <body>

    <h1>Clan Name</h1>

  </body>

</html>

The string would be stored as "htmlOutput" and then I need to upload that string to a directory on a web-server using a variable as the file-name. 该字符串将被存储为“ htmlOutput”,然后我需要使用一个变量作为文件名将该字符串上载到Web服务器上的目录中。 For example the file-name might be 495731951.html 例如,文件名可能是495731951.html

How is this achieved? 如何实现的?

I've had a look at jsftp but there's nothing for uploading the strings value. 我看过jsftp,但是没有什么可以上传字符串值的。 I don't want to have to create a new file on the local machine for each file as there will be a lot of files. 我不想在本地计算机上为每个文件创建一个新文件,因为会有很多文件。

Thank you! 谢谢!

I assume that you have an access to that remote server. 我假设您有权访问该远程服务器。 Instead of using FTP I'll suggest to write a simple PHP script that handles POST requests and saves a file. 我建议不要编写简单的PHP脚本来处理POST请求并保存文件,而不是使用FTP。

// save.php
$html = isset($_POST['content']) ? $_POST['content'] : '';
if($html !== '') {
  $file = 'file.txt';
  file_put_contents($file, $content);
}

I'm suggesting PHP because I guess that it is supported there. 我建议使用PHP,因为我猜想那里支持它。 I again, assume that there is no Node.js on that server. 我再次假设该服务器上没有Node.js。

Once you have such file there ( save.php ) you may send your variable with the request module. 一旦在那里有了这样的文件( save.php ),您可以将变量与请求模块一起发送

request.post('http://service.com/save.php', {
  form: {
    content: htmlOutput
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM