簡體   English   中英

nativescript:使用 ajax(獲取)發布請求上傳使用相機拍攝的圖像

[英]nativescript: Upload image taken with camera using ajax (fetch) post request

我對 nativescript 很陌生,但已經非常喜歡它了。 手頭的問題是:我想在手機上拍照,然后使用POST請求上傳到web服務器。

要從相機中獲取圖像,我遵循了本教程: https://nativescript.org/blog/take-your-selfie-with-nativescript-and-its-cross-platform-camera-api/

具體代碼:

cameraModule.takePicture().then(function (picture) {
   imageContainer.imageSource = picture;
});

我現在將圖像存儲在一個變量中,並顯示在用戶界面中(非常酷)。 現在我想使用 fetch 模塊將圖像上傳到網絡服務器: https://docs.nativescript.org/ns-framework-modules/fetch

我想修改網站上發布請求的代碼,但不確定如何添加圖片:

fetch("https://httpbin.org/post", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
        img : WHAT SHOULD I ADD HERE?
    })
}).then((r) => r.json())
    .then((response) => {
        const result = response.json;
    }).catch((e) => {
});

據我了解,還有一個 HTTP 模塊( https://docs.nativescript.org/ns-framework-modules/http ),所以我不確定哪個最適合這份工作。 取指或 HTTP 模塊。

使用 fetch 發送文本很容易,並且已經可以使用。 像這樣的東西:

body: JSON.stringify({
    text : "yay this is some text"
})

但是我一般如何發送圖像或文件?

非常感謝您!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM