electron访问ftp服务的图片

1. 用FileZilla Server Interface搭建一个ftp服务
2.如果是IE浏览器 可以直接访问
  <img alt="111"src="ftp://dzjg:dzjg@192.168.0.221:14147/1.jpg"/>

3.在electron 中是谷歌浏览器内核就得先下载下图片,转成base64

const ftp = require('basic-ftp')
class BufferWritable extends Writable {
  constructor() {
    super({ objectMode: false })
    this.data = Buffer.alloc(0)
  }

  _write(chunk, encoding, callback) {
    this.data = Buffer.concat([this.data, chunk])
    callback()
  }

  _final(callback) {
    callback()
  }

  getBuffer() {
    return this.data
  }
}

async downloadImageAsBinary(ftpConfig, remoteFilePath) {
      const client = new ftp.Client()
      client.ftp.verbose = true // 可选:启用详细日志记录

      try {
        await client.access({
          host: ftpConfig.host,
          port: ftpConfig.port,
          user: ftpConfig.user,
          password: ftpConfig.password,
          secure: false // 根据你的 FTP 服务器是否使用 SSL/TLS 来设置
        })

        const writable = new BufferWritable()
        await client.downloadTo(writable, remoteFilePath)

        const imageData = writable.getBuffer()
        // 现在 imageData 是一个包含图片二进制数据的 Buffer 
        return `data:image/jpeg;base64,${imageData.toString("base64")}`
      } catch (err) {
        console.error(err)
        throw err
      } finally {
        client.close()
      }
  },

  

posted @ 2025-06-18 10:25  福超  阅读(10)  评论(0)    收藏  举报