ionic 2将文件对象上传到服务器

时间:2017-06-29 09:48:29

标签: angular file-upload upload ionic2

我必须将文件作为文件上传到服务器,方法是将文件传递给名为' picture'的参数。

这是我的代码。

imageUpload(){ 
        let actionSheet = this.actionSheetCtrl.create({
            title: 'Select Image Source',
            buttons: [
            {
                text: 'Load from Library',
                handler: () => {
                    this.image11(this.camera.PictureSourceType.PHOTOLIBRARY);
                }
            },
            {
                text: 'Use Camera',
                handler: () => {
                    this.image11(this.camera.PictureSourceType.CAMERA);
                }
            },
            {
                text: 'Cancel',
                role: 'cancel'
            }
            ]
        });
        actionSheet.present();
    }

这是我的上传功能:

       correctPath:any
       currentName:any


       image11(sourceType) {

        // Create options for the Camera Dialog
        var options = {
            quality: 100,
            sourceType: sourceType,
            saveToPhotoAlbum: false,
            correctOrientation: true,
            destinationType: 1
        };
        this.camera.getPicture(options).then((imagePath) => {

            console.log ('image path ' + imagePath)

            //need to handle special cases in Android 
            if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
                this.filePath.resolveNativePath(imagePath)
                .then(filePath => {
                    console.log ('file path is ' + filePath)
                    this.correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
                    this.currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
                });
            } else {
                this.currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
                this.correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
            }



            (<any>window).resolveLocalFileSystemURL(imagePath, (fileEntry) => {

                fileEntry.file((resFile) => {

                    var reader = new FileReader();
                    reader.onloadend = (evt: any) => {
                        var imgBlob: any = new Blob([evt.target.result], { type: 'image/jpeg' });
                        console.log (JSON.stringify(imgBlob) + 'blob')

                    };

                    reader.onerror = (e) => {
                        console.log('Failed file read: ' + e.toString());

                    };

                    console.log (JSON.stringify(resFile)  +'res file')
                    reader.readAsArrayBuffer(resFile);


                    let options1: FileUploadOptions = {
                        fileKey: "file",
                        fileName: this.currentName,
                        chunkedMode: false,
                        mimeType: "multipart/form-data",
                        params:{picture:resFile}
                    }

                    const fileTransfer: TransferObject = this.transfer.create();

                    fileTransfer.upload(resFile.localURL, "http://jhp-dev-har2vey.codeanyapp.com/jhp/public/index.php/api/upload", options1).then(data => {
                        console.log ( "data at file upload " + JSON.stringify(data))
                    }, err => {
                        console.log (JSON.stringify(err) + 'fileupload error')
                    });
                }, (err)=>console.log ('error at file entery ' + JSON.stringify(err)));

            });//window 

        }, (err) => {
            console.log ('erroar at getting picture ' + JSON.stringify(err))
        })//camera imagepath
       }

它没有传入实际文件,而是传入包含文件名,位置等的字符串。

0 个答案:

没有答案