Image Nativescript上载图像

Image Nativescript上载图像,image,file-upload,image-uploading,nativescript,Image,File Upload,Image Uploading,Nativescript,嗨,我想拍张照片并上传到我的服务器上。下面是我的nativescript代码 这是我上传图片的请求代码 一旦我运行我的应用程序并拍摄照片并将其发送到服务器。服务器返回一条错误消息,如下所示 注意:这是我用于nativescript应用程序的标题 var headers = {"Content-Type": "multipart/form-data"} 在nativescript中还有文件系统模块的createReadStream()方法吗?您可以使用nativescript后台http插

嗨,我想拍张照片并上传到我的服务器上。下面是我的nativescript代码

这是我上传图片的请求代码

一旦我运行我的应用程序并拍摄照片并将其发送到服务器。服务器返回一条错误消息,如下所示

注意:这是我用于nativescript应用程序的标题

var headers = {"Content-Type": "multipart/form-data"}

在nativescript中还有文件系统模块的createReadStream()方法吗?

您可以使用
nativescript后台http
插件。 下面是你如何使用它

var bghttp = require("nativescript-background-http");
var session = bghttp.session("image-upload");

// save the image somewhere
var folder = fs.knownFolders.documents();
var pathOfImage = fs.path.join(folder.path, "Test.png");
var saved = image.saveToFile(pathOfImage,".png");
var request = 
        url : uploadPhotoUrl,
        method : "POST",
        headers : {
            "Content-Type" : "application/octet-stream"
        }
    };
var task = session.uploadFile(pathOfImage,request);
编辑

在后端(nodejs+express),您可以实现中间件功能并使用该功能:

var rawParser = bodyParser.raw({
    type: 'application/octet-stream', extended: true
});

app.post("/photoUpload",rawParser,function(req,res){
var path = "uploads/test.jpg";
var file = fs.writeFileSync(path,req.body);
});

下面是一个文件上载到QuickBlox的示例(它在引擎盖下使用AWS S3,支持“多部分/表单数据”)


gist上的完整代码

请参见:Hey@Nathanel尝试了您的方法,但服务器似乎抛出了一个错误,指出application/base64也是一个错误的内容类型。log(files.avatar)仍然抛出未定义的:(在服务器端,您需要取消base64格式。使用我的方法,图像作为base64文本文件传输。@Nathanel顺便说一句,如果您想使用“表单”,我在服务器上使用了强大的库。)数据;然后您需要使用最新版本的nativescript background http,因为我最近添加了将表单值上载到该代码库的功能。下面Aaron的代码应该可以工作。尝试了nativescript background http它仍然不工作服务器仍然抛出未定义的错误:(您如何使用摄像头模块获取图像的路径?您能澄清一些事情吗?路径已定义但从未使用过。它用于什么?nativescript uploader代码正在执行POST方法,但express代码是get方法。express get route何时调用?@zero upadted回答:)@RaymondCamden您可以使用像nativescript mediafilepicker这样的插件来返回文件路径
    var fs = require("tns-core-modules/file-system");
    var imagepicker = require("nativescript-imagepicker");
    var bghttp = require("nativescript-background-http");
    var parseUri = require("parseUri");


    var ile = fs.File.fromPath(this.filePath);
    var size = file.readSync().length;
    var contentType = 'image/jpeg';

    var uri = parseUri(createResult.blob_object_access.params),
       uploadUrl = uri.protocol + "://" + uri.authority + uri.path;

    var uploadParams = [];
    Object.keys(uri.queryKey).forEach(function(val) {
       uploadParams.push({name: val, value: decodeURIComponent(uri.queryKey[val])});
    });
    uploadParams.push({name: "file", filename: self.filePath, mimeType: contentType}); // If the element is named anything other than 'file', you're likely to receive a MaxPostPreDataLengthExceededError response when upload to S3.

    var request = {
        url: uploadUrl,
        method: "POST"
    };

    var session = bghttp.session("image-upload");
    var task = session.multipartUpload(uploadParams, request);

    function onEvent(e) {
        console.log("event: " + e.eventName);
        if(e.object.description){
          console.log(e.object.description);
        }
        if(e.error){
          console.log(e.error);
        }
        if(e.currentBytes){
          console.log(e.currentBytes + " of " + e.totalBytes);
        }
        if(e.data){
          console.log(e.data);
        }
    }

    task.on("progress", onEvent.bind(this));
    task.on("error", onEvent.bind(this));
    task.on("responded", onEvent.bind(this));
    task.on("complete", onEvent.bind(this));
});