如何在Azure中使用Node JS功能上传文件

编程入门 行业动态 更新时间:2024-10-06 11:25:21
本文介绍了如何在Azure中使用Node JS功能上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建一个Azure函数来处理文件上传.我尝试了其他选项(尝试直接从请求中读取或使用强大的功能).

I am trying to create an Azure function that handles file upload. I have tried different options (trying to read from request directly or using formidable).

对于这两种情况,执行函数时都会出现以下错误.

For both these cases I am getting following error when the function is executed.

Exception while executing function: Functions.UploadFile. mscorlib: TypeError: req.on is not a function at IncomingForm.parse (D:\home\site\wwwroot\node_modules\formidable\lib\incoming_form.js:117:6) at module.exports (D:\home\site\wwwroot\UploadFile\index.js:5:10) at D:\Program Files (x86)\SiteExtensions\Functions\1.0.11702\bin\azurefunctions\functions.js:106:24.

功能代码如下

var formidable = require("formidable"); module.exports = function (context, request) { context.log('JavaScript HTTP trigger function processed a request.'); var form = new formidable.IncomingForm(); form.parse(request, function (err, fields, files) { context.res = { body : "uploaded"}; }); context.done(); };

感谢您的帮助.

推荐答案

我了解了以下内容.在Azure函数中(以及在AWS lambda中),请求对象既不是Stream也不是EventEmitter.它只是具有正文和标头填充.我从 www.npmjs/package/parse-multipart.我必须针对Azure功能进行调整

I got it to work with following. Request object is neither a Stream nor an EventEmitter in Azure functions (and in AWS lambda too). It just has body and headers populated. I took help from www.npmjs/package/parse-multipart. I had to tune it for Azure functions

var multipart = require("parse-multipart"); module.exports = function (context, request) { context.log('JavaScript HTTP trigger function processed a request.'); // encode body to base64 string var bodyBuffer = Buffer.from(request.body); // get boundary for multipart data e.g. ------WebKitFormBoundaryDtbT5UpPj83kllfw var boundary = multipart.getBoundary(request.headers['content-type']); // parse the body var parts = multipart.Parse(bodyBuffer, boundary); context.res = { body : { name : parts[0].filename, type: parts[0].type, data: parts[0].data.length}}; context.done(); };

这似乎与Azure Function 2.x运行时(测试版)更好地配合.我已经更新了代码.我已经用PDF,JPG,PNG和XLSX对此进行了测试.

This seems to work better with Azure Function 2.x runtime (beta). I have updated the code. I have tested this with PDF, JPG, PNG and XLSX.

更多推荐

如何在Azure中使用Node JS功能上传文件

本文发布于:2023-11-26 18:52:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634772.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传文件   功能   如何在   Node   Azure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!