Skip to content

File upload fails: is not valid file #15

@terreb

Description

@terreb

Uploading fails with "Application error: Error /Users/user/Library/Developer/CoreSimulator/Devices/11C75134-AC52-46B8-87F6-58A61B8A1E0C/data/Containers/Data/Applicatio ... 282.jpeg is not a valid file:// url undefined" on iOS emulator. The path is correct and a valid image can be found at this path.

tns-core-modules: 1.7.1
tns-ios : 2.0.0
tns-android: 2.0.0
nativescript-background-http: 0.0.3

The code snippet:

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 base64String = imageSource.toBase64String(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)
        }
    }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions