-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add support for FormData
#621
Comments
|
FormData
Are you planning on importing it from WebKit or building a custom implementation? The API seems pretty minimal (if we would not support the optional |
I found we can patch import multipart from 'parse-multipart-data';
import { FormData } from 'formdata-polyfill/esm.min.js';
Request.prototype.formData = async function formData() {
const boundary = multipart.getBoundary(this.headers.get('content-type'));
const buffer = Buffer.from(await new Response(this.body).text());
const parts = multipart.parse(buffer, boundary);
const form = new FormData();
for (let i = 0; i < parts.length; i++) {
form.append(parts[i].name, parts[i].data);
}
return form;
}; Interestingly enough, calling |
@pateketrueke Can you help me with https://github.com/gornostay25/svelte-adapter-bun? I try to add polyfill... like |
@gornostay25 just add // node_modules/@sveltejs/kit/src/exports/node/polyfills.js"
import { FormData } from "formdata-polyfill/esm.min.js";
+import multipart from "parse-multipart-data";
/** @type {Record<string, any>} */
const globals = {
FormData,
};
export default function installPolyfills() {
+ Request.prototype.formData = async function formData() {
+ const boundary = multipart.getBoundary(this.headers.get('content-type'));
+ const buffer = Buffer.from(await new Response(this.body).text());
+ const parts = multipart.parse(buffer, boundary);
+ const form = new FormData();
+
+ for (let i = 0; i < parts.length; i++) {
+ form.append(parts[i].name, parts[i].data);
+ }
+ return form;
+ };
for (const name in globals) {
Object.defineProperty(globalThis, name, {
enumerable: true,
configurable: true,
writable: true,
value: globals[name],
});
}
} Also make sure you're including a polyfill for globalThis.File = class File {
constructor(bytes, filename, options = {}) {
Object.defineProperties(this, {
name: { value: filename },
type: { value: options.type },
size: { value: bytes.length },
lastModified: { value: options.lastModified || null },
lastModifiedDate: { value: options.lastModified ? new Date(options.lastModified) : null },
arrayBuffer: { value: () => new TextEncoder().encode(bytes) },
text: { value: () => Promise.resolve(bytes.toString()) },
[Symbol.toStringTag]: { value: 'File' },
});
}
}; |
- [`Request.prototype.formData`](https://www.npmjs.com/package/parse-multipart-data) - [`File`](oven-sh/bun#621 (comment)) Update packages Some fixes
Implemented in #2051 |
Serialize FormData but missing content-type "boundary"? @Jarred-Sumner |
Can you advice how to use it ? #621 (comment) @Jarred-Sumner |
Understood now. But 2 awaits cause a prob: 500 Internal Server Error
|
I've created a middleware for uploading multipart form data.
|
Version
0.1.3
Platform
Linux 4b2918398c1b 5.15.0-1012-gcp #17~20.04.1-Ubuntu SMP Thu Jun 23 16:10:34 UTC 2022 x86_64 GNU/Linux
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
Always
What is the expected behavior?
https://developer.mozilla.org/en-US/docs/Web/API/Request/formData
What do you see instead?
Additional information
No response
The text was updated successfully, but these errors were encountered: