首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何访问fastify中上载文件的文件路径

如何访问fastify中上载文件的文件路径
EN

Stack Overflow用户
提问于 2022-03-12 11:57:57
回答 1查看 1.1K关注 0票数 2

当使用表单上载某些文件时,我可以在网络检查器的dev tools中看到,特别是在form data下的“有效负载”选项卡中,在view source中。

注意,下面包括文件名,包括路径twoItems/Screenshot...,它是我需要在API中访问的这个路径twoItems,但不能访问。

保安?为什么我想要这个?这是一个文档管理应用程序,用户不能在浏览器中创建文件夹,然后添加文件。它们需要拖放嵌套的文件目录。

代码语言:javascript
复制
------WebKitFormBoundarydJ6knkAHgNW7SIF7
Content-Disposition: form-data; name="file"; filename="twoItems/Screenshot 2022-03-11 at 08.58.24.png"
Content-Type: image/png


------WebKitFormBoundarydJ6knkAHgNW7SIF7
Content-Disposition: form-data; name="file"; filename="twoItems/Screenshot 2022-03-11 at 08.58.08.png"
Content-Type: image/png

因此,在API中,我有一个标准的fastify API正在运行。

代码语言:javascript
复制
mport Fastify, { FastifyInstance, RouteShorthandOptions } from "fastify";
import { Server, IncomingMessage, ServerResponse } from "http";

const fs = require("fs");
const util = require("util");
const { pipeline } = require("stream");
const pump = util.promisify(pipeline);

const fastify: FastifyInstance = Fastify({});
fastify.register(require("fastify-multipart"));
fastify.register(require("fastify-cors"), {
  methods: ["GET", "PUT", "POST"],
});

const dir = "./files";
if (!fs.existsSync(dir)) {
  fs.mkdirSync(dir);
}

fastify.post("/upload", async (req: any, reply) => {
  console.log(req);
  const parts = await req.files();
  for await (const part of parts) {
    console.log(part); //---------------- LOG BELOW
    await pump(part.file, fs.createWriteStream(`./files/${part.filename}`));
  }
  reply.send();
});

const start = async () => {
  try {
    await fastify.listen(3001);
    const address = fastify.server.address();
    const port = typeof address === "string" ? address : address?.port;
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};
start();

我找不到如何访问每个项目的路径

当我退出part的时候.

代码语言:javascript
复制
<ref *1> {
  fieldname: 'file',
  filename: 'Screenshot 2022-03-11 at 17.52.11.png',
  encoding: '7bit',
  mimetype: 'image/png',
  file: FileStream {
    _readableState: ReadableState {
      objectMode: false,
      highWaterMark: 16384,
      buffer: BufferList { head: [Object], tail: [Object], length: 4 },
      length: 208151,
      pipes: [],
      flowing: null,
      ended: false,
      endEmitted: false,
      reading: false,
      sync: false,
      needReadable: false,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: true,
      destroyed: false,
      errored: null,
      closed: false,
      closeEmitted: false,
      defaultEncoding: 'utf8',
      awaitDrainWriters: null,
      multiAwaitDrain: false,
      readingMore: false,
      dataEmitted: false,
      decoder: null,
      encoding: null,
      [Symbol(kPaused)]: null
    },
    _events: [Object: null prototype] {
      end: [Function (anonymous)],
      limit: [Function (anonymous)]
    },
    _eventsCount: 2,
    _maxListeners: undefined,
    bytesRead: 208151,
    truncated: false,
    _read: [Function (anonymous)],
    [Symbol(kCapture)]: false
  },
  fields: { file: [ [Object], [Object], [Object], [Circular *1] ] },
  _buf: null,
  toBuffer: [AsyncFunction: toBuffer]
}

这是未定义的..。

代码语言:javascript
复制
console.log(part.path);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-12 18:09:42

你需要设置服务生的选项:

代码语言:javascript
复制
fastify.register(require("fastify-multipart"), {
  preservePath: true
});

您可以在这里找到所有选项:https://github.com/fastify/busboy#busboy-methods

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71449504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档