问题
I'm trying to take a picture with NativeScript Camera module and then upload it with nativescript-background-http (since as I understand this is the only way of uploading in NS at this moment). I'm using iOS simulator, but get an error on Android emulator too.
Camera plugin works fine, takes and saves a picture to a file. However I have a problem to upload the picture from the path after that.
Here is my code:
import cameraModule = require('camera')
import imageModule = require('ui/image')
import enumsModule = require('ui/enums')
import fsModule = require('file-system')
import bgHttpModule = require('nativescript-background-http')
const options = { width: 300, height: 300, keepAspectRatio: true }
const format = enumsModule.ImageFormat.jpeg
cameraModule.takePicture(options).then(imageSource => {
let contentType = `image/${format}`
let savePath = fsModule.knownFolders.documents().path
let fileName = 'img_' + new Date().getTime() + '.' + format
let filePath = fsModule.path.join( savePath, fileName )
if ( imageSource.saveToFile( filePath, format ) ) {
var session = bgHttpModule.session('image-upload')
var options = {
url: 'http://192.168.99.100:8003',
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream',
'File-Name': fileName
},
description: '{ \'uploading\': ' + fileName + ' }'
}
let task = session.uploadFile(filePath, options)
task.on('progress', logEvent)
task.on('error', logEvent)
task.on('complete', logEvent)
function logEvent(e) {
console.log(e.eventName)
}
}
})
The error I get is:
Application error: Error /Users/user/Library/Developer/CoreSimulator/Devices/11C75134-AC52-46B8-87F6-58A61B8A1E0C/data/Containers/Data/Applicatio ... 538.jpeg is not a valid file:// url undefined
However if I go to that path the picture with that name is there and valid. Am I doing something wrong?
回答1:
I was able to get rid of this error by prepending "file://" to the task line
var task = session.uploadFile("file://" + this.filePath, request);
It works in the emulator but I haven't tested it on an actual device yet.
来源:https://stackoverflow.com/questions/37388774/nativescript-camera-takepicture-and-upload-with-nativescript-background-http