我需要从供应商的Web服务中检索文件并将它们推送到一个独特的blob容器中,以便用户拥有一个独特的“工作区”.基本上,我会从供应商那里获取文件,用户可以通过他们自己的blob容器中的文件编辑这些文件,这样他们就不会互相交换工作文件.我有独特的blob容器工作,但需要从我的供应商API“下载”/获取文件并将它们推送到blob容器中.我能够成功检索文件,这将是单独的调用以获取PDF,文本文件和图像…但是如果我尝试将它们上传到Azure Blob存储,我在Node.js中收到以下错误:
TypeError: Cannot read property ‘length’ of null
我想我需要在客户端将文件编码为base64以正确获取长度,并且已经看到了使用Canvas和toDataURL的一些示例,但我不确定这是否是实际下载和直接推送到的最佳方法Azure Blob存储,特别是因为我有PDF等文件(不确定PDF是否可以进行base64编码).
这是调用服务的AngularJS控制器(注意实际端点可能会根据它们调用的文件而改变,所以我使用客户端GET文件来控制用户可以在表单中输入的变量):
- $scope.getFiles = function () {
- $.ajax({
- url: 'http://vendorwebservice.net/ws/file1',type: "GET",success: function (result) {
- console.log(result);
- var filename = 'Texture_0.png';
- $http.post('/postFile',{ filename: filename,file: result }).success(function (data) {
- console.log(data);
- },function (err) {
- console.log(err);
- });
- alert("Files Retrieved!");
- },error: function (error) {
- console.log("Failed to download image!");
- }
- })
- }
这是我的后端/ Node / Express代码:
- app.post('/postFile',function (req,res,next) {
- var filename = req.body.filename;
- var file = req.body.file;
- var base64Data;
- fileBuffer = decodeBase64Image(file);
- blobSvc.createBlockBlobFromText('blob5',filename,fileBuffer.data,{ 'contentType': fileBuffer.type },function (error,result,response) {
- if (!error) {
- console.log("Uploaded" + result);
- }
- else {
- console.log(error);
- }
- });
- })
- // Decode file for upload
- function decodeBase64Image(dataString) {
- var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),response = {};
- if (matches.length !== 3) {
- return new Error('Invalid input string');
- }
- response.type = matches[1];
- response.data = new Buffer(matches[2],'base64');
- return response;
- }
更新1:
根据Gary的建议,我尝试了以下内容,但由于我的供应商API没有文件URI,而是在GET上生成文件的端点,因此搞砸了代码(也就是说,我在如何通过端点而不是Gary的有意义的例子).例如,我的供应商端点’http://vendorapi.net/ws/texture_0‘返回名为“Texture_0.png”的文件.
前端角度代码:
- $scope.getFromVendor = function () {
- var filename = 'Texture_0.png';//jpg,txt...
- $http.post('/uploadapifiles',url: 'http://vendorapi.net/ws/texture_0' }).success(function (data) {
- console.log(data);
- },function (err) {
- console.log(err);
- });
- }
服务器端下载处理(我相信这是最混乱的一个:
- app.get(http://vendorapi.net/ws/texture_0',next) {
- res.download('http://vendorapi.net/ws/texture_0' + req.params.filename);
- })
服务器端上传处理:
- app.post('/uploadapifiles',next) {
- var filename = req.body.filename;
- var r = request(req.body.url).pipe(fs.createWriteStream('http://vendorapi.net/ws/texture_0' + filename))
- r.on('close',function () {
- blobsrv.createBlockBlobFromLocalFile('blob5','http://vendorapi.net/ws/texture_0' + filename,response) {
- if (!error) {
- console.log("Uploaded" + result);
- }
- else {
- console.log(error);
- }
- });
- })
- });
在您最初的想法中,首先您在客户端获取文件内容数据,然后将数据发布到Express Web服务器.
如果你得到的文件很大,它会减慢你的网站速度,因为文件数据将通过HTTP传输两次,并且可能会出现其他问题.
所以我尝试了另一种想法作为解决方法.
我只是将获取特定文件的API发布到服务器,将文件保存为服务器目录中的文件并将文件上载到服务器端的存储.
这是我的代码片段:
角度前端:
- $scope.upload =function(){
- var filename = (new Date()).getTime()+'.pdf';//jpg,txt...
- $http.post('http://localhost:1337/uploadfile',url: 'http://localhost:1337/output/123.pdf'}).success(function (data) {
- console.log(data);
- },function(err){
- console.log(err);
- });
- }
后端:
我怀疑你获得文件格式的API就像落后了.
- router.get('/output/:filename',next) {
- res.download('upload/'+req.params.filename);
- })
post请求处理程序利用包request,并且没有必要确定文件类型或编码类型,createBlockBlobFromLocalFile将在blob存储上提供的位置上传文件,API reference:
- router.post('/uploadfile',next) {
- var request = require('request');
- var filename = req.body.filename;
- var tmpFolder = 'upload/',//this folder is to save files download from vendor URL,and should be created in the root directory prevIoUsly.
- tmpFileSavedLocation = tmpFolder + filename; //this is the location of download files,e.g. 'upload/Texture_0.png'
- var r = request(req.body.url).pipe(fs.createWriteStream(tmpFileSavedLocation))//this Syntax will download file from the URL and save in the location asyns
- r.on('close',function (){
- blobsrv.createBlockBlobFromLocalFile('vsdeploy',tmpFileSavedLocation,response) {
- if (!error) {
- console.log("Uploaded" + result);
- }
- else {
- console.log(error);
- }
- });
- })
- })