gpt4 book ai didi

cordova - 将文件从 Cordova 应用程序流式传输到服务器

转载 作者:行者123 更新时间:2023-12-01 04:45:04 29 4
gpt4 key购买 nike

我有cordova应用程序,用户必须能够从手机中选择图像并将其上传到AWS S3。我正在使用 cordova-plugin-camera以及用于此的 S3 SDK。我有工作正常的代码(见下文),但需要将整个图像拉入 base64 编码字符串(每 Camera.DestinationType.DATA_URL )内的内存,然后再次拉入 Buffer .

我更愿意将其从手机的硬盘驱动器中传输出来,以减轻潜在的内存问题。 cordova-plugin-camera网站甚至对此有警告:

/** * Warning: Using DATA_URL is not recommended! The DATA_URL destination * type is very memory intensive, even with a low quality setting. Using it * can result in out of memory errors and application crashes. Use FILE_URI * or NATIVE_URI instead. */



那么有没有什么方法可以使用 FILE_URI 将照片直接从磁盘传输到 S3?或 NATIVE_URI或任何其他技术?

这是我当前的代码:
doTheThing() {
let options = {
quality: 50,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
encodingType: Camera.EncodingType.JPG
};

navigator.camera.getPicture(imgData => {
this.uploadToS3(imgData);
}, options);


uploadToS3(data) {
let AWS = require('aws-sdk');

AWS.config = new AWS.Config();
AWS.config.accessKeyId = 'noway';
AWS.config.secretAccessKey = 'jose';
AWS.config.region = 'us-east-2';

let s3 = new AWS.S3();
let uploadParams = { Bucket: 'iamkule/f1/f2', Key: '', Body: new Buffer(data, 'base64') };
uploadParams.Key = 'myfile.jpg';

s3.upload(uploadParams, function (err, data) {
if (data) {
console.log('yay!');
}
});
}
}

最佳答案

这应该让你开始。请原谅我的伪代码。根据需要随意编辑它。

设备将图像保存到磁盘:

function takePicture(urlCallback){
let options = {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,// <--
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
encodingType: Camera.EncodingType.JPG
};
navigator.camera.getPicture(urlCallback, onFail, options);
function onFail(message) {
alert('Failed because: ' + message);
}
}

从磁盘获取图像作为 blob:
function getImageAsBlob(url, blobCallback) {
var xhr = new XMLHttpRequest();
xhr.open( "GET", url, true );
xhr.responseType = "arraybuffer";
xhr.onload = function( ev ) {
// Obtain a blob: URL for the image data.
var arrayBufferView = new Uint8Array( this.response );
var blob = new Blob( [ arrayBufferView ], { type: "image/jpeg" } );
blobCallback(blob);
};
xhr.send();
}

将 blob 上传到 S3:
function uploadToS3(blob, callback) {
let AWS = require('aws-sdk');
AWS.config = new AWS.Config();
AWS.config.accessKeyId = 'noway';
AWS.config.secretAccessKey = 'jose';
AWS.config.region = 'us-east-2';
let s3 = new AWS.S3();
let options = { Bucket: 'iamkule/f1/f2', Key: 'myfile.jpg', Body: blob };// <--
s3.upload(options, callback);
}

把它们放在一起:
takePicture(function(url){
getImageAsBlob(url, function(blob){
uploadToS3(blob, function (err, data) {
if (data) console.log('yay!');
});
});
});

S3 信息来自 here .图片拍摄信息来自 here .
快乐编码!

关于cordova - 将文件从 Cordova 应用程序流式传输到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47340351/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com