From 20ffcbd035dfb8cd5c472abe7924087c2d9b5d5b Mon Sep 17 00:00:00 2001 From: franciscop-sc <47550791+franciscop-sc@users.noreply.github.com> Date: Wed, 6 Oct 2021 16:41:51 +0900 Subject: [PATCH 1/2] Added example to transform FormData to zip Added example to transform a FormData instance to zip, considering we want all of the file fields to be added to the zip --- documentation/howto/formdata_to_zip.md | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 documentation/howto/formdata_to_zip.md diff --git a/documentation/howto/formdata_to_zip.md b/documentation/howto/formdata_to_zip.md new file mode 100644 index 00000000..f8cc599b --- /dev/null +++ b/documentation/howto/formdata_to_zip.md @@ -0,0 +1,38 @@ +--- +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: + +``` + +$('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. From 4e58b3116bab2c1f3e54f52736e423bdf1b886bd Mon Sep 17 00:00:00 2001 From: franciscop-sc <47550791+franciscop-sc@users.noreply.github.com> Date: Wed, 6 Oct 2021 16:42:34 +0900 Subject: [PATCH 2/2] Update formdata_to_zip.md --- documentation/howto/formdata_to_zip.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/documentation/howto/formdata_to_zip.md b/documentation/howto/formdata_to_zip.md index f8cc599b..a41eff01 100644 --- a/documentation/howto/formdata_to_zip.md +++ b/documentation/howto/formdata_to_zip.md @@ -17,8 +17,7 @@ $(form).on('submit', e => { 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);