我尝试使用NativeScript Camera模块拍摄照片,然后使用nativescript-background-http上传(因为我知道这是此时在NS上传的唯一方式)。我使用的是iOS模拟器,但在Android模拟器上也出现了错误。
相机插件工作正常,拍摄并将照片保存到文件中。但是我有一个问题是在那之后从路径上传图片。
这是我的代码:
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)
}
}
})
我得到的错误是:
应用程序错误:错误/用户/用户/库/开发人员/ CoreSimulator / Devices / 11C75134-AC52-46B8-87F6-58A61B8A1E0C / data / Containers / Data / Applicatio ... 538.jpeg不是有效文件:/ / url undefined
但是,如果我去那条路,那个名字的照片就在那里并且有效。我做错了吗?
答案 0 :(得分:1)
我能够通过预先添加" file://"来摆脱这个错误。到任务线
using System;
using Excel = Microsoft.Office.Interop.Excel;
namespace Helpers.Vsto
{
public sealed class WorkbookClosedMonitor
{
internal class CloseRequestInfo
{
public CloseRequestInfo(string name, int count)
{
this.WorkbookName = name;
this.WorkbookCount = count;
}
public string WorkbookName { get; set; }
public int WorkbookCount { get; set; }
}
public WorkbookClosedMonitor(Excel.Application application)
{
if (application == null)
{
throw new ArgumentNullException("application");
}
this.Application = application;
this.Application.WorkbookActivate += Application_WorkbookActivate;
this.Application.WorkbookBeforeClose += Application_WorkbookBeforeClose;
this.Application.WorkbookDeactivate += Application_WorkbookDeactivate;
}
public event EventHandler<WorkbookClosedEventArgs> WorkbookClosed;
public Excel.Application Application { get; private set; }
private CloseRequestInfo PendingRequest { get; set; }
private void Application_WorkbookDeactivate(Excel.Workbook wb)
{
if (this.Application.Workbooks.Count == 1)
{
// With only one workbook available deactivating means it will be closed
this.PendingRequest = null;
this.OnWorkbookClosed(new WorkbookClosedEventArgs(wb.Name));
}
}
private void Application_WorkbookBeforeClose(Excel.Workbook wb, ref bool cancel)
{
if (!cancel)
{
this.PendingRequest = new CloseRequestInfo(
wb.Name,
this.Application.Workbooks.Count);
}
}
private void Application_WorkbookActivate(Excel.Workbook wb)
{
// A workbook was closed if a request is pending and the workbook count decreased
bool wasWorkbookClosed = true
&& this.PendingRequest != null
&& this.Application.Workbooks.Count < this.PendingRequest.WorkbookCount;
if (wasWorkbookClosed)
{
var args = new WorkbookClosedEventArgs(this.PendingRequest.WorkbookName);
this.PendingRequest = null;
this.OnWorkbookClosed(args);
}
else
{
this.PendingRequest = null;
}
}
private void OnWorkbookClosed(WorkbookClosedEventArgs e)
{
var handler = this.WorkbookClosed;
if (handler != null)
{
handler(this, e);
}
}
}
public sealed class WorkbookClosedEventArgs : EventArgs
{
internal WorkbookClosedEventArgs(string name)
{
this.Name = name;
}
public string Name { get; private set; }
}
}
它可以在模拟器中运行,但我还没有在实际设备上测试它。