patrickjuchli / basic-ftp

FTP client for Node.js, supports FTPS over TLS, passive mode over IPv6, async/await, and Typescript.
MIT License
673 stars 89 forks source link

uploadfrom and appendFrom TypeError: source.once is not a function #239

Closed Themp10 closed 1 year ago

Themp10 commented 1 year ago

Describe the bug this a router that connects to a ftp server, /list is working but /uploads is not

Example code `const express = require('express'); const router = express.Router(); const ftp = require('basic-ftp'); const multer = require('multer')

// Create an FTP client const client = new ftp.Client();

router.use(async (req, res, next) => { try { console.log("Connecting to the FTP server...."); await client.access({ host: 'myhostname.net', user: 'user', password: 'password' }); console.log("Connected to the FTP server!"); next(); } catch (error) { console.error('FTP Connection Error:', error); res.status(500).json({ error: 'Failed to connect to FTP server' }); } });

// Route to list files router.get('/list', async (req, res) => { try { const list = await client.list(); res.json(list); } catch (error) { res.status(500).json({ error: error }); } });

router.post('/upload', async (req, res) => { try { // Use Multer to handle file uploads const upload = multer({ storage: multer.memoryStorage() }).single('file'); upload(req, res, async function (err) { if (err) { return res.status(400).json({ error: 'File upload failed' }); }

  const fileData = req.file;

  if (!fileData) {
    return res.status(400).json({ error: 'No file selected' });
  }
  console.log(fileData)
  console.log(fileData.buffer)

  await client.append(fileData.buffer, fileData.originalname);
  res.status(200).json({ message: 'File uploaded successfully' });
});

} catch (error) { console.error('Error uploading file:', error.message); res.status(500).json({ error: error.message }); } });

// Close the FTP client when it's no longer needed router.use((req, res, next) => { client.close(); console.log("FTP client closed."); next(); }); // Other FTP routes go here

module.exports = router; ` Console output

/home/ouss/file-explorer/api/node_modules/basic-ftp/dist/Client.js:391 source.once("error", onError); ^

TypeError: source.once is not a function at Client._uploadFromStream (/home/ouss/file-explorer/api/node_modules/basic-ftp/dist/Client.js:391:16) at Client._uploadWithCommand (/home/ouss/file-explorer/api/node_modules/basic-ftp/dist/Client.js:366:21) at Client.appendFrom (/home/ouss/file-explorer/api/node_modules/basic-ftp/dist/Client.js:357:21) at Client.append (/home/ouss/file-explorer/api/node_modules/basic-ftp/dist/Client.js:723:21) at /home/ouss/file-explorer/api/ftp.router.js:52:20 at done (/home/ouss/file-explorer/api/node_modules/multer/lib/make-middleware.js:45:7) at indicateDone (/home/ouss/file-explorer/api/node_modules/multer/lib/make-middleware.js:49:68) at Multipart. (/home/ouss/file-explorer/api/node_modules/multer/lib/make-middleware.js:166:7) at Multipart.emit (node:events:513:28) at emitCloseNT (node:internal/streams/destroy:132:10)

Node.js v18.14.0

Which version of Node.js are you using? e.g. Node 1.11.0

Additional context I tried every thing i found in this repo but nothing resolves the error im new to file stream so i dont really get whats the problem here

patrickjuchli commented 1 year ago

Are you providing a stream to the upload function?

patrickjuchli commented 1 year ago

Closing due to inactivity.

rfdt commented 1 year ago

Are you providing a stream to the upload function?

I have the same trouble? From xlsx lib i have Buffer. Then i convert buffer to readable stream and i had the same error. And type ReadableStream retrun this "Argument of type 'ReadableStream ' is not assignable to parameter of type 'string | Readable'." If I set excel_stream as any. basic-ftp return error.

image image
patrickjuchli commented 1 year ago

You need to provide a Readable stream as provided by Node (https://nodejs.org/api/stream.html). Not a ReadableStream as provided by the Web API (https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).