Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the contentType of multipart field. #974

Closed
msojocs opened this issue Apr 3, 2024 · 2 comments
Closed

Support the contentType of multipart field. #974

msojocs opened this issue Apr 3, 2024 · 2 comments

Comments

@msojocs
Copy link

msojocs commented Apr 3, 2024

Support plan

  • which support plan is this issue covered by? (e.g. Community, Sponsor, or
    Enterprise):Community
  • is this issue currently blocking your project? (yes/no):yes
  • is this issue affecting a production system? (yes/no):no

Context

  • node version:
  • module (formidable) version: v18.18.2
  • environment (e.g. node, browser, native, OS): node
  • used with (i.e. popular names of modules): express
  • any other relevant information:

What problem are you trying to solve?

const form = formidable({
        uploadDir: storagePath,
        createDirsFromUploads: true,
        keepExtensions: true,
    });
    form.parse<'chatType' | 'peerUid' | 'guildId', "file">(req, (err, fields, files) => {
        if (err) {
            log.error('file parse error:', err)
            next(err);
            return;
        }

            log.info('fields:', fields);
            log.info('files:', files);
    })

If the 'Content-Type' was set, formidable wil parse the field as file.

raw data:

POST http://127.0.0.1:8081/group/uploadFile HTTP/1.1
User-Agent: Apifox/1.0.0 (https://apifox.com)
Accept: */*
Host: 127.0.0.1:8081
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------420820768465130987175823
Content-Length: 279

----------------------------420820768465130987175823
Content-Disposition: form-data; name="chatType"

2
----------------------------420820768465130987175823
Content-Disposition: form-data; name="peerUid"
Content-Type: text/plain; charset=UTF-8

933286835
----------------------------420820768465130987175823--

result:

[2024-04-03 16:55:10.629] [info] [UploadFile] fields: { chatType: [ '2' ] }
[2024-04-03 16:55:10.629] [info] [UploadFile] files: {
  peerUid: [
    PersistentFile {
      _events: [Object: null prototype],
      _eventsCount: 1,
      _maxListeners: undefined,
      lastModifiedDate: 2024-04-03T08:55:10.629Z,
      filepath: 'D:\\GitHub\\Yukihana\\tmp\\storage\\7a8a74440325b47b92e55aa03',
      newFilename: '7a8a74440325b47b92e55aa03',
      originalFilename: null,
      mimetype: 'text/plain; charset=UTF-8',
      hashAlgorithm: false,
      size: 9,
      _writeStream: [WriteStream],
      hash: null,
      [Symbol(kCapture)]: false
    }
  ]
}

As you can see, when I set the contentType of field named peerUid(text/plain), it will be parsed as file.

Maybe we can just supprt the contentType text and file.

Do you have a new or modified API suggestion to solve the problem?

@msojocs msojocs closed this as completed Apr 3, 2024
@msojocs
Copy link
Author

msojocs commented Apr 3, 2024

#875

@msojocs
Copy link
Author

msojocs commented Apr 3, 2024

const form = formidable({
        uploadDir: storagePath,
        createDirsFromUploads: true,
        keepExtensions: true,
    });
    form.onPart =  function (part) {
        log.info('part:', part)
        if (part.mimetype && !(part as any).headers['content-disposition']?.match(/filename="/)) {
            // we want it to be a "field" (cf https://github.com/node-formidable/formidable/issues/875 )
            part.mimetype = null
        }
        form._handlePart(part);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant