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

Added example to transform FormData to zip #795

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions documentation/howto/formdata_to_zip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: "How to create a zip from a FormData object"
layout: default
section: example
---

When submitting a `multipart/form-data`, a FormData can be generated automatically:

```js
$(form).on('submit', e => {
e.preventDefault();
const form = new FormData(e.target);

// ...
});
```

To convert from the FormData to zip all of the files, you can parse it easily and add all of the files to the zip:

```js
$('form#myform').on('submit', e => {
e.preventDefault();
const form = new FormData(e.target);

const zip = new JSZip()
for (let [key, value] of form.entries()) {
if (value instanceof File) {
zip.file(key, value);
form.delete(key); // No longer needed it here
}
}

// ...
});
```

Now you have all of the files uploaded in the form within a single zip, and can proceed with it as you desire.