Git Product home page Git Product logo

imagepicker's Introduction

ImagePicker

Ionic2+AngularJS2 图片选择component和多图上传

利用RxJS上传多张图片

/***
   * 上传多张图片
   * @param params: 服务器上传图片api所需参数
   * @param filePaths: 通过拍照或者相册拿到的本地图片url数组
   * @returns {Observable}: 服务器返回的图片url
   */
  uploadImages(params: any, filePaths: Array<string>):Observable<any> {
    var options: FileUploadOptions = {
      fileKey: 'filedata',
      fileName: 'avatar.jpg',
      chunkedMode: false,
      mimeType : "image/jpeg",
      params: params
    };

    //每个图片上传任务创建一个信号
    var observables: Array<any> = [];
    for (var  i = 0; i < filePaths.length; ++i) {
      let filePath = filePaths[i];
      var observable = new Observable((sub:any) => {
        const fileTransfer = new Transfer();
        let url = '图片上传url';
        fileTransfer.upload(filePath, url, options)
          .then((data: FileUploadResult) => {
            if (200 === data.responseCode) {
              var response = JSON.parse(data.response);
              // 图片上传成功,后台会返回一个图片imageUrl字符串
              var imageUrl = response.data;
              // 将该url发送出来
              sub.next(imageUrl);
              sub.complete();
            } else {
              sub.error('上传图片失败!');
            }
          }, (error) => {
            sub.error(error);
          });
      });
      observables.push(observable);
    }

    return ForkJoinObservable.create(observables);
  }

imagepicker's People

Contributors

schubertq avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.