简体   繁体   English

Node.js / Azure:将HTML上传到BlobStorage

[英]Node.js/Azure: upload HTML to BlobStorage

I have a frontend which sends the HTML of that page to a Node.js server. 我有一个前端,它将页面的HTML发送到Node.js服务器。 The server should then send that HTML to Azure BlobStorage. 然后,服务器应将该HTML发送到Azure BlobStorage。

Here is my express route to handle this: 这是我处理此问题的捷径:

router.post("/sendcode", function(req, res) {
  let code = "";
  code = req.body.code;
  console.log(code);
  let service = storage.createBlobService(process.env.AccountName, process.env.AccountKey);
  service.createContainerIfNotExists("htmlcontainer", function(error, result, response) {
    if (error) {
      throw error;
    } else {
      service.createBlockBlobFromStream("htmlcontainer", code, function(err, result, response) {
        if (err) {
          throw err;
        } else {
          console.log(result);
          console.log(response);
        }
      });
    }
  });
});

When I call this route, I receive this in my console: 当我调用此路由时,会在控制台中收到此消息:

<html><style>* { box-sizing: border-box; } body {margin: 0;}</style><body></body></html>

How can I send it to BlobStorage? 如何将其发送到BlobStorage? Avoid the method I used as it maybe wrong because I can't figure out what function to use because of scarce documentation. 避免使用我使用的方法,因为它可能是错误的,因为文档稀少,我无法弄清楚要使用的功能。

关于堆栈溢出有很多答案,您可以按照此答案并根据需要进行调整: 使用node.js在Azure文件存储中上传文件

So after trying a little bit, i was able to send it not from stream but after writing it to a file, sending that file through createBlockBlobFromLocalFile function in azure-storage module and deleting the file from disk. 因此,在尝试了一点之后,我能够将其从流中发送出去,而不是从流中发送,而是将其写入文件中,然后通过azure-storage模块中的createBlockBlobFromLocalFile函数发送该文件, createBlockBlobFromLocalFile从磁盘中删除该文件。

I know this isnt the most efficient method out there but it got my work done. 我知道这不是最有效的方法,但是它完成了我的工作。

The problem i was facing was that after uploading html code in a block blob(without file operation), its content type was application/octet which i wanted to be text/html. 我面临的问题是,在将html代码上传到块blob中后(没有文件操作),其内容类型为application / octet,我希望是text / html。 So i had to perform one more operation to change its content type, which i found to be a little difficult due to unavailability of documentation and examples in javascript. 因此,我不得不再执行一次操作来更改其内容类型,由于没有javascript的文档和示例,我发现这有点困难。

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

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