nativescript-background-http问题

时间:2018-10-11 09:31:52

标签: nativescript nativescript-telerik-ui

我尝试为我的应用实现图片上传页面,但是很遗憾,请求没有到达服务器。我在服务器端使用tcpdump进行了仔细检查。代码似乎无法处理sendImages函数中的 session.uploadFile

请让我知道代码是否有问题

var imageSource = require("image-source");
var frameModule = require("ui/frame");
var Observable = require("data/observable").Observable;
var fromObject = require("data/observable").fromObject;
var ObservableArray = require("data/observable-array").ObservableArray;
var platformModule = require("platform");

var permissions = require("nativescript-permissions");
var imagepickerModule = require("nativescript-imagepicker");
var bghttpModule = require("nativescript-background-http");
var session = bghttpModule.session("image-upload");

var fs = require("file-system");


var page;
var imageName;
var counter = 0;

function pageLoaded(args) {
    page = args.object;
}


function onSelectSingleTap(args) {
    var context = imagepickerModule.create({
        mode: "single"
    });

    if (platformModule.device.os === "Android" && platformModule.device.sdkVersion >= 23) {
        permissions.requestPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE, "I need these permissions to read from storage")
            .then(function () {
                console.log("Permissions granted!");
                startSelection(context);
            })
            .catch(function () {
                console.log("Uh oh, no permissions - plan B time!");
            });
    } else {
        startSelection(context);
    }
}

function sendImages(uri, fileUri) {

    imageName = extractImageName(fileUri);
    var request = {
        url: "http://maskingIpForPost:8081/mobilepic/ctk/uploadpic",
        method: "POST",
        headers: {
            "Content-Type": "application/octet-stream",
            "file-Name": imageName,
            "uid": 30
        },
        description: "{ 'uploading': " + imageName + " }"
    };

    var task = session.uploadFile(fileUri, request);

    task.on("progress", progress);
    task.on("error", error);
    task.on("complete", complete);
    task.on("responded", responded);
    function responded(e) {
        console.log("eventName: " + e.eventName);
        console.log("data: " + e.data);
    }
    function progress(e) {
        console.log("currentBytes: " + e.currentBytes);
        console.log("totalBytes: " + e.totalBytes);
        console.log("eventName: " + e.eventName);
    }
    function error(e) {
        console.log("eventName: " + e.eventName);
        console.log("eventName: " + e.responseCode);
        console.log("eventName: " + e.response);
    }

    function complete(e) {
        console.log("eventName: " + e.eventName);
        console.log("response: " + e.responseCode);
   }

    return task;
}

function startSelection(context) {

    context
        .authorize()
        .then(function () {

            return context.present();
        })
        .then(function (selection) {
            selection.forEach(function (selected_item) {
                    var localPath = null;

                    if (platformModule.device.os === "Android") {
                        localPath = selected_item._android;
                    } else {
                        // selected_item.ios for iOS is PHAsset and not path - so we are creating own path
                        let folder = fs.knownFolders.documents();
                        let path = fs.path.join(folder.path, "Test" + counter + ".png");
                        //let saved = imagesource.saveToFile(path, "png");

                        localPath = path;
                    }
alert("final path  " + localPath);
                    if (localPath) {
                        var task = sendImages("Image" + counter + ".png", localPath);
                        //mainViewModel.get("items").push(fromObject({ thumb: imagesource, uri: "Image" + counter + ".png", uploadTask: task }));
                    }
                    counter++;
            });
        }).catch(function (e) {
            console.log(e.eventName);
        });
}

function extractImageName(fileUri) {
    var pattern = /[^/]*$/;
    var imageName = fileUri.match(pattern);

    return imageName;
}


exports.pageLoaded = pageLoaded;
exports.onSelectSingleTap = onSelectSingleTap;

关于进一步的研究发现以下内容

net.gotev.uploadservice.UploadService在background-http.android.js中未定义。目前,我不确定这意味着什么。如果有人对此有想法,将不胜感激

2 个答案:

答案 0 :(得分:0)

您需要在代码中更改以下行。

var session = bghttpModule.session("image-upload");

必须是文件上传

var session = bghttpModule.session("file-upload");

只需使用我身边的Azure Blob存储PUT网址测试您的代码,并获得以下响应。

ActivityManager: START u0 {act=android.intent.action.OPEN_DOCUMENT typ=image/* cmp=com.android.documentsui/.DocumentsActivity (has extras)} from pid 2835

JS: currentBytes: 4096
JS: totalBytes: 25220
JS: eventName: progress
JS: currentBytes: 25220
JS: totalBytes: 25220
JS: eventName: progress
JS: currentBytes: 25220
JS: totalBytes: 25220
JS: eventName: progress
JS: eventName: responded
JS: data: 
JS: eventName: complete
JS: response: 201

答案 1 :(得分:0)

感谢快速响应,我尝试在模拟器上运行它,但遇到了上述问题,我通过连接设备进行了同样的尝试,效果很好。