# How to upload multiple files using file type input

**URL:** <https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905>\
**Category:** ionic-v3\
**Created:** [August 4, 2017, 7:47pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905 "2017-08-04T19:47:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![gz0023](https://avatars.discourse-cdn.com/v4/letter/g/9fc348/32.png) [@gz0023](https://forum.ionicframework.com/u/gz0023)\
**Post date:** [August 4, 2017, 7:47pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/1 "2017-08-04T19:47:35Z")

</div>

I’m trying to upload multiple files but I don’t know how to. I can successfully upload a file or image using the code below but not multiple files. How can I upload multiple files instead?

```auto
<input type="file" (change)="onUploadChange($event)" #fileInput multiple/>

```

```auto
 @ViewChild("fileInput") fileInput: ElementRef;
formData = new FormData();

private readFile(file: any) {
  const reader = new FileReader();
  reader.onloadend = () => {
      const imgBlob = new Blob([reader.result], {type: file.type});
      formData.append('file', imgBlob, file.name);
  };
  reader.readAsArrayBuffer(file);
}

onUploadChange(ev) {
  let myFile = ev.target.files[0];
  //let url = URL.createObjectURL(myFile);
  this.readFile(myFile);

```

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 4, 2017, 7:48pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/2 "2017-08-04T19:48:50Z")

</div>

I would start by adding the `multiple` attribute.

---

<div class="post-metadata">

**Author:** ![gz0023](https://avatars.discourse-cdn.com/v4/letter/g/9fc348/32.png) [@gz0023](https://forum.ionicframework.com/u/gz0023)\
**Post date:** [August 4, 2017, 7:49pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/3 "2017-08-04T19:49:39Z")

</div>

I can already select multiple files but I’ll add it

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [August 4, 2017, 8:17pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/4 "2017-08-04T20:17:13Z")

</div>

What does upload mean? To an external database?

---

<div class="post-metadata">

**Author:** ![gz0023](https://avatars.discourse-cdn.com/v4/letter/g/9fc348/32.png) [@gz0023](https://forum.ionicframework.com/u/gz0023)\
**Post date:** [August 4, 2017, 8:23pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/5 "2017-08-04T20:23:16Z")

</div>

I’ll be uploading them to my server. I already have that part working I dont know how to process multiple selected files

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 4, 2017, 8:27pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/6 "2017-08-04T20:27:53Z")

</div>

> [@gz0023](#):
>
> I dont know how to process multiple selected files

You seem to have a hardcoded `[0]` in your handler. Can’t you just loop across the entire array instead?

---

<div class="post-metadata">

**Author:** ![gz0023](https://avatars.discourse-cdn.com/v4/letter/g/9fc348/32.png) [@gz0023](https://forum.ionicframework.com/u/gz0023)\
**Post date:** [August 4, 2017, 8:52pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/7 "2017-08-04T20:52:30Z")

</div>

Thanks… I got how to loop

```auto
onUploadChange(ev) {
  let myFile = ev.target.files;
  //let url = URL.createObjectURL(myFile);

  for (let i=0; i<myFile.length; i++) {
    this.readFile(myFile[i]);
  }

```

But I should have been more clear. What I want to do is if a user uploads 3 items I want to append each to `formData` so that I can later send all 3 to my server at the same time. Should I use FormData for this or is there a better way?

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 4, 2017, 8:54pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/8 "2017-08-04T20:54:07Z")

</div>

> [@gz0023](#):
>
> Should I use FormData for this or is there a better way?

Personally, I consider JSON far superior to FormData, in part because of how simple it makes this sort of issue, as it has built-in support for arrays. In the end, this is all completely dependent on what the server is capable of accepting.

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [August 4, 2017, 9:03pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/9 "2017-08-04T21:03:03Z")

</div>

I think you should consider using ng2-file-upload. It’s recommended by a lot of places, for example Cloundinary uses it in their official Angular 2 sample project. Your code looks very Javascripty, and not very Angular-2’'y. The more you can Angularize it, the better off you’ll be. Also, making your code Observable-based instead of callback-based might simplify things a lot.

---

<div class="post-metadata">

**Author:** ![gz0023](https://avatars.discourse-cdn.com/v4/letter/g/9fc348/32.png) [@gz0023](https://forum.ionicframework.com/u/gz0023)\
**Post date:** [August 4, 2017, 9:03pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/10 "2017-08-04T21:03:34Z")

</div>

I’m using PHP as my backend so basically anything is acceptable

This is my first time doing file upload. How would I convert each file to json?

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 4, 2017, 9:08pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/11 "2017-08-04T21:08:09Z")

</div>

> [@gz0023](#):
>
> How would I convert each file to json?

Three options I can think of, ordered from easiest and most bandwidth-using to hardest and most compact:

- substitute `readAsDataURL()` where you currently have `readAsArrayBuffer()`. Data URLs can safely be transported as JSON strings.
- do the base64 encoding of your array buffer by hand
- use [UBJSON](http://ubjson.org/)

---

<div class="post-metadata">

**Author:** ![gz0023](https://avatars.discourse-cdn.com/v4/letter/g/9fc348/32.png) [@gz0023](https://forum.ionicframework.com/u/gz0023)\
**Post date:** [August 4, 2017, 9:39pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/12 "2017-08-04T21:39:07Z")

</div>

So first the first way after I change that line instead of appending to formdata would I do something like

```auto
data = [];

onUploadChange(ev) {
  let myFile = ev.target.files;
  //let url = URL.createObjectURL(myFile);

  for (let i=0; i<myFile.length; i++) {
    this.readFile(myFile[i]);
  }

private readFile(file: any) {
  const reader = new FileReader();
  reader.onloadend = () => {
      this.data.push(reader.result, file.type);
  };
  reader.readAsArrayDataURL(file);
}

```

And for the second option I’m not really sure how I would do that… I found this example online

```auto
var bufferToBase64 = function (buffer) {
    var bytes = new Uint8Array(buffer);
    var len = buffer.byteLength;
    var binary = "";
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode(bytes[i]);
    }
    return window.btoa(binary);
};

```

Would the buffer be my `data` array from above?

---

<div class="post-metadata">

**Author:** ![gz0023](https://avatars.discourse-cdn.com/v4/letter/g/9fc348/32.png) [@gz0023](https://forum.ionicframework.com/u/gz0023)\
**Post date:** [August 4, 2017, 9:52pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/13 "2017-08-04T21:52:59Z")

</div>

Looking through how `ng2-file-upload` works at the end of the day I think I would bestuck in the same place trying to figure out how to send multiple files in one request

---

<div class="post-metadata">

**Author:** ![testtest1212](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@testtest1212](https://forum.ionicframework.com/u/testtest1212)\
**Post date:** [November 4, 2017, 5:49am UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/14 "2017-11-04T05:49:35Z")

</div>

Hi @gz0023

Did you found any solution on this issue?

I’m also encountering this kind of issue.  
I don’t know how to upload multiple files.

Can you help me?

Many Thanks!

Kind Regards,

---

<div class="post-metadata">

**Author:** ![praveengoud](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@praveengoud](https://forum.ionicframework.com/u/praveengoud)\
**Post date:** [November 5, 2018, 12:15pm UTC](https://forum.ionicframework.com/t/how-to-upload-multiple-files-using-file-type-input/100905/15 "2018-11-05T12:15:46Z")

</div>

hi bro,  
did find any solution for that issues,  
I am also facing same problem.
