node.js - Angular 6 和 Node Js AWS S3 文件上传进度条

标签 node.js angular amazon-s3

我试图在通过 Angular 6(前端)和 NodeJs(后端)将文件上传到 asw-s3 存储桶时创建一个进度条。 如何获取进度(或已上传的字节)并在 Angular 6 前端实时接收它们?

最佳答案

不确定 Angular 是否有具体的方法,但这是我在一些基于 Angular 的网站中使用的一个工作示例:

sendFile(file) {
    let formData: FormData = new FormData();
    formData.append('my_file', file);

    let xhr = new XMLHttpRequest();
    xhr.upload.onprogress = function (progEvent: ProgressEvent) {
        if (progEvent.lengthComputable) {
            var uploadedSoFar = (progEvent.loaded / progEvent.total) * 100;
            console.log("Uploaded: " + uploadedSoFar + "% ")
            if (progEvent.loaded == progEvent.total){
                // uploaded up to 100%
            }
        }

    };
    xhr.open("POST", `/your_site`, true);
    xhr.send(formData);
}

对正在发生的事情的一些解释:

表单数据

The FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data"

https://developer.mozilla.org/en-US/docs/Web/API/FormData

XMLHttpRequest

Use XMLHttpRequest (XHR) objects to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing.

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

Node 端 我正在更新这篇文章以添加 Node 代码示例(在评论之后)。我不太擅长node.js,所以我的以下代码不是一个很好的例子。

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  // Remove this, I use it for this example to be easy to reproduce
  res.setHeader('X-Frame-Options', 'ALLOWALL');
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'POST, GET');
  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  if (req.method == 'POST') {
        console.log("Receiving file");
        var body = '';
        req.on('data', function (data) {
            body += data;
            console.log("receiving data : " + body);
        });
        req.on('end', function () {
            console.log("received all the data: " + body);
        });
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end('Reception completed');
    }

});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

如果您在 Node.js 中收到数据,这意味着您的前端工作正常。

关于node.js - Angular 6 和 Node Js AWS S3 文件上传进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51176537/

相关文章:

node.js - 需要 Express 应用程序中的子模块

node.js - 使用 FilterExpression 在 GSI 中进行 DynamoDB 查询

angular - 如何在 `mat-datepicker` 中添加清除按钮

javascript - Angular 2中的密码确认

javascript - 精细上传到 S3 文件夹

amazon-web-services - 使用 s3-dist-cp 跨 AWS 账户复制 S3 文件

node.js - 如何使用 next.js 构建博客?

javascript - meteor :ReferenceError:要求未定义

javascript - 使用 Angular2 上传文件

amazon-web-services - 使用适用于 Java API 的 AWS 开发工具包时出现 SSLHandshakeException