nativescript:使用 ajax(获取)post 请求上传相机拍摄的图像
nativescript: Upload image taken with camera using ajax (fetch) post request
我对 nativescript 还很陌生,但已经非常喜欢它了。手头的问题是:我想在手机 phone 上拍照并使用 POST 请求将其上传到网络服务器。
要从相机获取图像,我遵循了本教程:
https://nativescript.org/blog/take-your-selfie-with-nativescript-and-its-cross-platform-camera-api/
具体代码:
cameraModule.takePicture().then(function (picture) {
imageContainer.imageSource = picture;
});
我现在将图像存储在一个变量中并显示在用户界面中(非常酷)。现在我想使用获取模块将图像上传到网络服务器:
https://docs.nativescript.org/ns-framework-modules/fetch
我想为网站上的 post 请求修改代码,但不确定如何添加图像:
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"
})
但一般情况下如何发送图像或文件?
非常感谢您!
我对 nativescript 还很陌生,但已经非常喜欢它了。手头的问题是:我想在手机 phone 上拍照并使用 POST 请求将其上传到网络服务器。
要从相机获取图像,我遵循了本教程: https://nativescript.org/blog/take-your-selfie-with-nativescript-and-its-cross-platform-camera-api/
具体代码:
cameraModule.takePicture().then(function (picture) {
imageContainer.imageSource = picture;
});
我现在将图像存储在一个变量中并显示在用户界面中(非常酷)。现在我想使用获取模块将图像上传到网络服务器: https://docs.nativescript.org/ns-framework-modules/fetch
我想为网站上的 post 请求修改代码,但不确定如何添加图像:
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"
})
但一般情况下如何发送图像或文件?
非常感谢您!