Next.js Discord

Discord Forum

Upload File via FTP

Unanswered
Dwarf Crocodile posted this in #help-forum
Open in Discord
Dwarf CrocodileOP
I have a form and I need to transfer the file via FTP protocol to my own separate server. What's the best way to do this? I was suggested to use Curl, but how can I insert a long query string into the form and get the submission result?
curl -T "C:\Projects\curl\file_name.pdf" -u "username:password" ftp://example.com:port/folder/

7 Replies

You can try using package basic-ftp
on npmjs
Dwarf CrocodileOP
I tried it, but it gives me a 500 error, although the files are successfully sent through Curl or an FTP client.
Dwarf CrocodileOP
// pages/api/upload.js

'use client'
import * as formidable from 'formidable';
import ftp from 'basic-ftp';

export const config = {
  api: {
    bodyParser: false,
  },
};

const uploadHandler = async (req, res) => {
  const form = new formidable.IncomingForm();
  
  form.parse(req, async (err, fields, files) => {
    if (err) {
      return res.status(500).json({ error: 'Error parsing form data' });
    }

    const file = files.file;

    try {
      const client = new ftp.Client();
      client.ftp.verbose = true;

      await client.access({
        host: process.env.NEXT_PUBLIC_FTP_HOST,
        user: process.env.NEXT_PUBLIC_FTP_USERNAME,
        password: process.env.NEXT_PUBLIC_FTP_PASSWORD,
        secure: false,
        port: process.env.NEXT_PUBLIC_FTP_PORT
      });

      await client.uploadFrom(file.filepath, `/ftpopt/${file.originalFilename}`);
      client.close();

      return res.status(200).json({ message: 'File uploaded successfully' });
    } catch (error) {
      return res.status(500).json({ error: 'Error uploading file via FTP' });
    }
  });
};

export default uploadHandler;

TypeError: formidableWEBPACK_IMPORTED_MODULE_0.default.IncomingForm is not a constructor
Why have you added "use client" at the top?
@Dwarf Crocodile I'm requesting updates on this issue