NativeScript: Camera takePicture and upload with nativescript-background-http

岁酱吖の 提交于 2019-12-24 15:21:39

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!