NativeScript:相机takePicture并使用nativescript-background-http上传 [英] NativeScript: Camera takePicture and upload with nativescript-background-http
问题描述
我正在尝试使用NativeScript Camera模块拍照,然后使用nativescript-background-http上传(因为据我所知,这是目前在NS中上传的唯一方法).我正在使用iOS模拟器,但在Android模拟器上也会出现错误.
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.
这是我的代码:
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)
}
}
})
我得到的错误是:
应用程序错误:错误/Users/用户/库/Developer/CoreSimulator/Devices/11C75134-AC52-46B8-87F6-58A61B8A1E0C/data/Containers/Data/Applicatio ... 538.jpeg不是有效文件://网址未定义
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?
推荐答案
我可以通过在任务行前添加"file://"来消除此错误
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.
这篇关于NativeScript:相机takePicture并使用nativescript-background-http上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!