Ionic Upload Image to Server

2019-08-31 02:53发布

I need to upload captured image and album image to the server in ionic app. Is anyone know how to do that? I am just fresh in developing ionic app

2条回答
ら.Afraid
2楼-- · 2019-08-31 03:12

i am using cordova imagepicker(pick image from camera) and upload to s3 server.

 function getImageFromGallery(cb) {
    // console.log('getImageFromGallery');
    var options = {
        maximumImagesCount: 1,
        width: 1280, //width of image 
        height: 1280, // height of image 
        quality: 80
    };
    $cordovaImagePicker.getPictures(options)
        .then(function(results) {
            console.log(results);
        }, function(error) {
            alert(error);
        });
} 
   
    function uploadImage(imageDataURI) {
        // console.log('uploadImage');
        var fileURL = imageDataURI;
        var options = new FileUploadOptions();
        options.fileKey = "image";
        options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
        options.chunkedMode = true;
        options.method = 'POST';
        var params = {
            'image_type': 'food'
        };
        options.params = params;

        var ft = new FileTransfer();
        ft.upload(fileURL, encodeURI("http://xxx.in/api/upload_image"),
            viewUploadedPictures,
            function(error) {
                console.log(error);
            }, options);
        console.log('success');
    }
var viewUploadedPictures = function(response) {
    var res = response.response;
    var jres = JSON.parse(res);
    var imgUrl = jres.data.public_photo_url;
    console.log('new image url link', imgUrl);
} 

Note:- i am using REST api for image uploading "http://xxx.in/api/upload_image" * * Dependency Injection $upload* *

查看更多
Luminary・发光体
3楼-- · 2019-08-31 03:23

It is not really clear what you mean by uploading an image to the server with an ionic app.

A suggestion might be to convert the image to a Base64 string and send it to the server.

查看更多
登录 后发表回答