nativescript-background-http获取响应正文

时间:2016-07-20 14:24:43

标签: nativescript

我目前正在使用nativescript-background-http上传图片,我只是想知道有没有办法在发送图片后从服务器获取响应正文或响应标题?

2 个答案:

答案 0 :(得分:1)

以防有人在一年后遇到同样的问题:

task.on("responded", (e) => {
   JSON.parse(e.data)
}

答案 1 :(得分:0)

function sendImages(uri, fileUri) {

    imageName = extractImageName(fileUri);

    var request = {
        url: "http://httpbin.org/post",
        method: "POST",
        headers: {
            "Content-Type": "application/octet-stream",
            "File-Name": imageName
        },
        description: "{ 'uploading': " + imageName + " }"
    };

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

    task.on("progress", logEvent);
    task.on("error", logEvent);
    task.on("complete", logEvent);

    function logEvent(e) {      
        console.log("----------------");
        console.log('Status: ' + e.eventName);
        // console.log(e.object);
        if (e.totalBytes !== undefined) {
            console.log('current bytes transfered: ' + e.currentBytes);
            console.log('Total bytes to transfer: ' + e.totalBytes);
        }  
    }

    return task;
}

基于this demo