我正在为我的客户实施离线功能,该功能允许他们离线提交表单并稍后重新提交(主要用于无服务)。我希望允许他们使用故障保护功能通过电子邮件将数据发送给我们,以防万一提交被卡住。我使用Ionic Email Composer
插件进行摄影,如果我想发送JSON数据,一切都可以正常工作,但是如果我的用户只是发送CSV文件,我希望这样做。我不确定将JSON代码转换为CSV然后作为附件发送的最佳选择是什么。
.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ViewController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { EmailComposer } from '@ionic-native/email-composer';
import { Angular5Csv } from 'angular5-csv/Angular5-csv';//Testing
import { ReapService } from '../../services/reap-service';
@IonicPage()
@Component({
selector: 'page-offline-data',
templateUrl: 'offline-data.html',
})
export class OfflineDataPage {
private offlineData:any [] =[];
private isData:boolean = false;
private emailJSONData:any = [];
private test:any;
constructor(public navCtrl: NavController,
public navParams: NavParams,
public storage: Storage,
public viewCtrl: ViewController,
private emailComposer: EmailComposer,
public reap: ReapService) {
}
ionViewDidLoad(){
this.storage.get('offlineSubmission').then((data)=>{
this.offlineData=data;
console.log(data);
/************ TESTING *********************/
console.log(JSON.stringify(data));
for(let i=0;i<data.length;i++){
this.emailJSONData.push(data[i]["Info"]);
}
console.log(this.emailJSONData);
this.test = new Angular5Csv(this.emailJSONData,'Form data');
console.log(this.test.csv);
});
if(this.offlineData==[]){
this.isData = false;
console.log(this.isData);
}
else{
this.isData = true;
}
}
dismiss() {
this.viewCtrl.dismiss();
}
sendEmail(){
this.emailComposer.isAvailable().then((available: boolean) =>{
if(available) {
//Now we know we can send
}
},(err) => {
this.reap.presentToast('Error Emailing Data');
console.log(err);
});
let email = {
to: 'john@doe.com',
cc: 'jane@doe.comm',
//bcc: ['john@doe.com', 'jane@doe.com'],
attachments: [
'file://'+this.test+'.csv'//Where I want to include the csv
],
subject: 'Offline Data Submissions',
body: this.emailJSONData.toString(),//Currently sending the raw JSON in body
isHtml: false
};
// Send a text message using default options
this.emailComposer.open(email);
}
}
[{"Type":"SIG","Info":[{"personnel":{"ID":"1","FullName":"Test User","default":"false"}},{"signature":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWgAAACWCAYAAADzChIIAAAQzElEQVR4Xu2dCdB95RzH/yiVrVSyRGUdO5U9y3+amBZLpjApkn03iKipyVS2bCGThjQiazUkNbKUaRBlF1kSIiRFVCR8P+aeOl3vfe89d33OuZ/fzG/ec+99znN+z+d53u899znPcqM1mgQkIAEJFEngRkVGZVASkIAEJLBGgbYRSEACEiiUgAJdaMUYlgQkIAEF2jYgAQlIoFACCnShFWNYEpCABBRo24AEJCCBQgko0IVWjGFJQAISUKBtAxKQgAQKJaBAF1oxhiUBCUhAgbYNSEACEiiUgAJdaMUYlgQkIAEF2jYgAQlIoFACCnShFWNYEpCABBRo24AEJCCBQgko0IVWjGFJQAISUKBtAxKQgAQKJaBAF1oxhiUBCUhAgbYNSEACEiiUgAJdaMUYlgQkIAEF2jYgAQlIoFACCnShFWNYEpCABBRo24AEJCCBQgko0IVWjGFJQAISUKBtAxKQgAQKJaBAF1oxhiUBCUhAgbYNSEACEiiUgAJdaMUYlgQkIAEF2jYgAQlIoFACCnShFWNYEpCABBRo24AEJCCBQgko0IVWjGFJQAISUKBtAxKQgAQKJaBAF1oxhiUBCUhAgbYNSEACEiiUgAJdaMUYlgQkIAEF2jYgAQlIoFACCnShFWNYEpCABBRo24AEJCCBQgko0IVWjGFJQAISUKBtAxKQgAQKJaBAF1oxhiUBCUhAgbYNSEACEiiUgAJdaMUYlgQkIAEF2jYgAQlIoFACCnShFWNYEpCABBRo24AEJCCBQgko0IVWjGFJQAISUKBtAxKQgAQKJaBAF1oxhiUBCUhAgbYNSEACEiiUgAJdaMUYlgQkIAEF2jYgAQlIoFACCnShFWNYEpCABBRo24AEJCCBQgko0IVWjGFJQAISKEGgd+xVw2lWhwQkIAEJXE9g0QK9a0I5qVYhL8jx0VaQBCQgAQmsWbNogd4jlXD8ChXxsrz3XitIAhKQwDITWLRA3yzw91zlrv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oMeEm6Qz66e5cVLzFuBLrFWjEkCy01g7xT/2BUQnJr3do33j6XuLC0FurNVa8Ek0GoCGyf6SweUYKe8f1qrSzdi8Ar0iKBMJgEJLITAhrnq5Stc+YS896J4p0d7KNALaXNeVAISaEiASS1f6juHTWiZ9NK/xkfDrMtNrkCXWzdGJgEJ3JAAevWs+DG9t6/N300H3GF3gp0C3YlqtBASWCoCG6W0LFd6QZwHh501BbqzVWvBJCCBthNQoNteg8YvAQl0loAC3dmqtWASkEDbCSjQba9B45eABDpLQIHubNVaMAlIoO0EFOi216DxS0ACnSWgQHe2ai2YBCTQdgIKdNtr0PglIIHOElCgO1u1FkwCEmg7AQW67TVo/BKQQGcJKNCdrVoLJgEJtJ2AAt32GjR+CUigswQU6M5WrQWTgATaTkCBbnsNGr8EJNBZAgp0Z6vWgklAAm0n8F8G18umk/BCAgAAAABJRU5ErkJggg=="}],"Status":"Pending"}]
我目前正在使用Angular5csv进行测试,但是不确定是否可以使用Ionic File
或任何其他有用的插件,这些插件可能有助于满足我的需求。
答案 0 :(得分:0)
您可以使用cordova-plugin-file将csv文件下载到IOS设备文件系统中。您可以使用Email Composer插件中的附件属性来附加相同的文件路径。
答案 1 :(得分:0)
所以我终于找到了答案,就我而言,从我将要使用的复杂对象创建csv并不是一个理想的选择,但是这里有一个示例:
convertToCSV(offlineData) {
var csv: any = ''
var line: any = ''
var offlineLength = offlineData.length
console.log(offlineLength);
//Header
for (var i = 0; i < offlineLength; i++) {
var formType = offlineData[i]['Type'];
line += formType+ '\r\n\r\n';
var SpT = offlineData[i]['Info'].length
for (var k = 0; k < SpT; k++){
line += Object.keys(offlineData[i]['Info'][k]);
if (line != '') line += ';'
}
line += '\r\n\r\n';
}
csv += line + '\r\n';
//offlineData
for (let i = 0; i < offlineLength; i++) {
line = ''
for (var j = 0; j < SpT; j++) {
if (line != '') line += ';'
switch(offlineData[i]['GPS'][j]){
case offlineData[i]['GPS'][j] == 'ID':
line += offlineData[i]['GPS'][j]['ID'];
break;
case 'Lat':
line += 'Lat';
break;
case ['Lon']:
line += 'Lon';
break;
default:
line += 'Test';
break;
}
}
csv += line + '\r\n'
}
return csv
}
saveAsCsv() {
var csv: any = this.convertToCSV(this.csvData)
console.log(csv)
var fileName: any = "test.csv"
this.File.writeFile(this.File.dataDirectory, fileName, csv ,{ replace: true })
.then(() => {
let email = {
to: 'email@email',
attachments: [
this.file.dataDirectory + 'test.csv'
],
subject: 'subject',
body: 'body text...',
isHtml: true
};
this.emailComposer.open(email);
})
.catch((err) => {
console.error(err);
});
}
当然,这不是我在问题中使用的确切数据,但是convertToCSV
函数将允许您操纵对象,然后通过电子邮件将其吐到csv中。
我的答案的积分为here。