使用离子2上传图像

时间:2017-07-07 07:58:34

标签: angular typescript ionic2

我正在尝试使用离子2将图像上传到服务器。但我得到的回复说没有上传文件。我手动添加,最终部分边界或者它给出错误多部分:未找到边界

  

这是我的ts文件

import { Component } from '@angular/core';
import { NavController,AlertController } from 'ionic-angular';
import { Camera } from 'ionic-native';
import {Http,Headers} from '@angular/http';
import * as Variables from '../variables';


@Component({
  selector: 'page-m_sale',
  templateUrl: 'm_sale.html'
})
export class M_salePage {
  salesname : string;
  endDate:string;
  base64Image
  constructor(public navCtrl: NavController,public http:Http,public alertCtrl: AlertController) {

  }
  accessGallery(){
   Camera.getPicture({
     sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
     destinationType: Camera.DestinationType.DATA_URL,
     targetWidth:10,
     targetHeight:10
    }).then((imageData) => {
      this.base64Image = 'data:image/jpeg;base64,'+imageData;
     }, (err) => {
      console.log(err);
    });
  }

  newsale(){
    let input = new FormData();
      input.append('name','Sale23');
      input.append('endDate','2018.2.5');
      input.append('image',this.base64Image);

        let alert = this.alertCtrl.create({
      title: 'Error',
      subTitle: JSON.stringify(this.base64Image),
      buttons: ['OK']
        });
      alert.present();


      console.log(this.salesname);
      let headers = new Headers ();
      headers.set('Content-Type','multipart/form-data;boundary=abc');
      this.http.post('http://url/api/rep/url/add-sale',input,{headers:headers})
      .subscribe((res)=>{
        let alert = this.alertCtrl.create({
      title: 'Error',
      subTitle: JSON.stringify(res),
      buttons: ['OK']
        });
      alert.present();
      })

  }



}

0 个答案:

没有答案