Forked from ChrisFlannagan/nativescript-upload-pic-to-php.js
Created
September 19, 2017 08:54
-
-
Save dyazincahya/93c4daa6a9928f3fe8825a85db821980 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var enumsModule = require("ui/enums"); | |
var cameraModule = require("camera"); | |
var fs = require('file-system'); | |
var bghttp = require("nativescript-background-http"); | |
exports.takephoto = function(args) { | |
cameraModule.takePicture({width: 800, height: 800, keepAspectRatio: true}).then(function(picture) { | |
var savepath = fs.knownFolders.currentApp().path + "/saved_images"; | |
var filename = 'img_' + new Date().getTime() + '.jpg'; | |
var filepath = fs.path.join(savepath, filename); | |
var picsaved = picture.saveToFile(filepath, enumsModule.ImageFormat.jpeg); | |
if(picsaved) { | |
console.log("Saving"); | |
var session = bghttp.session("image-upload"); | |
var request = { | |
url: config.apiUrl, | |
method: "POST", | |
headers: { | |
"Content-Type": "application/octet-stream", | |
"File-Name": filename | |
}, | |
description: "{ 'uploading': '" + filename + "' }" | |
}; | |
var task = session.uploadFile("file://" + filepath, request); | |
task.on("progress", logEvent); | |
task.on("error", logEvent); | |
task.on("complete", logEvent); | |
function logEvent(e) { | |
console.log(e.eventName); | |
} | |
} else { | |
console.log("Failed To Save"); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment