简体   繁体   中英

Cloudinary File Upload Error

I'm writing a REST API on using Express.js. The API needs to accept a video file from the client and upload it to cloudinary. When I use the api to return the file back to the client (as a test) everything works perfectly. When I then try to upload the same file to cloudinary I get an error. the error says:

"file.match is not a function"

I'm not sure what file.match even is or why it is giving me a problem. If anyone else has had this issue how did you solve it? Below is the code that is giving me issues:

app.js

var express = require('express');
var formidable = require('express-formidable');
var app = express();

app.use(formidable());

var routes = require('./routes');
app.use('/routes', routes);

var port = process.env.PORT || 3000;

app.listen(port, function() {
  console.log('Express server is listening on port ' + port);
});

routes.js

var express = require('express');
var cloudinary = require('../cloudinary.js').cloudinary;
var router = express.Router();

router.post('/upload', function(req, res, next) {
  cloudinary.uploader.upload(req.files, function(result) {
    console.log(result);
  });
});

module.exports = router;

cloudinary.js

var cloudinary = require('cloudinary');

cloudinary.config({
  cloud_name: 'name',
  api_key: 'key',
  api_secret: 'secret'
});

module.exports.cloudinary = cloudinary;

I was able to solve the issue. It was not a problem on Cloudinary's end. The key was to only send the location of the file.

WORKING routes.js

var express = require('express');
var cloudinary = require('../cloudinary.js').cloudinary;
var router = express.Router();

router.post('/upload', function(req, res, next) {
  var fileGettingUploaded = req.files.fileToUpload.path;

  cloudinary.uploader.upload(fileGettingUploaded, function(result) {
    console.log(result);
  });
});

module.exports = router;

Did you try to specify the resource_type as video -

cloudinary.uploader.upload(req.files, 
    function(result) {console.log(result); },
    { resource_type: "video" });

If you're uploading images and videos you can use the auto as resource_type .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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