如何在一个请求中上传多个文件?

时间:2017-08-16 17:11:42

标签: javascript node.js angular typescript ionic3

我要将多张图片上传到服务器。

这是DataURL的数组。

items: {
    img_url : string
}[] = [];

我发现离子原生文件传输只能上传一个文件。

如何上传多个文件?

1 个答案:

答案 0 :(得分:0)

import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';

 uploadAllImage()
  {
    const fileTransfer:FileTransferObject = this.transfer.create();

    var i;

    var filetype;
    var itemtype;

    for(i=0; i<this.photos.length; i++)
    {

      //GET TYPE OF ITEM
      filetype = this.itemtypes[i];

      //GET MIME TYPE from itemtype
      switch(filetype)
      {
        case 'audio':
        {
          itemtype = 'audio/amr';
          break;
        }
        case 'video':
        {
          itemtype = 'video/quicktime';
          break;
        }
        case 'image':
        {
          itemtype = 'image/jpeg';
          break;
        }
        default:
        {
          return;
        }
      }


      //SET NAME
      var name ='pinglun_' + filetype;
      name = name + '#';
      name = name + this.photosName[i];

      console.log(name);

      //SET MIMETYPE
      let option: FileUploadOptions = {
        fileKey:'file',
        mimeType:itemtype,
        httpMethod:'POST',
        fileName:name
      };

      console.log('itemtype', itemtype);

      //DETERMINE FILE DEPENDING ON MIMETYPE
      if(filetype == 'image')
      {
        fileTransfer.upload(this.photos[i], encodeURI(localStorage.getItem('GlobalIP')+"/upload"),option).then((result)=>{

        },function(error)
        {

        });
      }
      else
      {

        console.log('FURL:' + this.fileurls[i]);

        fileTransfer.upload(this.fileurls[i], encodeURI(localStorage.getItem('GlobalIP')+"/upload"),option).then((result)=>{

        },function(error)
        {

        });
      }

    }
  }