科尔多瓦多图像上传与文件传输
Cordova multiple image upload with File Transfer
我正在做一个cordova和jquery移动项目。
我已经能够使用文件传输插件上传一张图像。现在我尝试上传以下 2 或 3 张图片。
这是 HTML 代码:
<label for="image">Pictures:</label>
<a href="" id="image1Button" class="ui-btn" onclick="getPhoto(pictureSource.PHOTOLIBRARY);">Get first picture</a><br>
<a href="" id="image2Button" class="ui-btn" onclick="getPhoto(pictureSource.PHOTOLIBRARY);" style="display:none;">Get second picture</a><br>
<a href="" id="image3Button" class="ui-btn" onclick="getPhoto(pictureSource.PHOTOLIBRARY);" style="display:none;">Get third picture</a><br>
<img id="image1" style="display:none;width:25%;">
<img id="image2" style="display:none;width:25%;">
<img id="image3" style="display:none;width:25%;">
<label for="title">Title</label>
<input data-clear-btn="true" name="title" id="title" value="" type="text">
<input value="Continue" type="submit" id="adButton">
以下是jquery代码:
multi_upload(user_id);
function multi_upload(user_id) {
var image1 = "image1";
var image2 = "image2";
var image3 = "image3";
if($('#image2').prop('src') == ''){
// upload one file
upload(user_id, image1, "true");
}
if($('#image3').prop('src') == ''){
// upload two files
upload(user_id, image1, "false");
upload(user_id, image2, "true");
}
if($('#image3').prop('src') != ''){
// upload three files
upload(user_id, image1, "false");
upload(user_id, image2, "false");
upload(user_id, image3, "true");
}
}
function upload(user_id, imagesrc, final) {
var img = '';
var imageURI = '';
img = document.getElementById(imagesrc);
imageURI = img.src;
console.log("[imageURI] "+imageURI);
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
var params = {};
params.timestamp = Math.round(+new Date()/1000);
params.public_token = localStorage.getItem("token_public");
params.hash = SHA1(params.timestamp+localStorage.getItem("token_private"));
params.user_id = user_id;
options.params = params;
options.chunkedMode = false;
var ft = new FileTransfer();
if(final == "true"){
ft.upload(imageURI, "http://www.example.com/api/index.php/privates/upload", finalwin, fail, options);
}else{
ft.upload(imageURI, "http://www.example.com/api/index.php/privates/upload", win, fail, options);
}
}
例如,如果我上传两个文件,代码将上传上一张所选图片的两倍。 控制台给了我看起来像这样的 imageURI:
file:///storage/sdcard0/Android/data/fr.myproject.propro/cache/modified.jpg?1418726649440:500
我想这是一个临时文件,所以我假设当我选择最后一个文件时,它会删除前一个文件......如何找到此图像的真实路径?
我们最近遇到了同样的问题,发现缓存文件(project/cache/modified.jpg
(被新选择覆盖(如您所注意的(,尽管FileTransfer.upload似乎将其视为两个不同的文件(可能是由于?-参数(,因此上传了两次。
作为一种解决方法,我们最终重命名了文件,以便在名称前包含时间戳,以便在上传文件之前modified.jpg?1418726649440
变得1418726649440modified.jpg
:
function renameFile(src, callback) {
var d = new Date();
//find the FileEntry for the file on the device
window.resolveLocalFileSystemURL(src, function(fileEntry) {
//get the parent directory (callback gives a DirectoryEntry)
fileEntry.getParent(function(parent) {
//rename the file, prepending a timestamp.
fileEntry.moveTo(parent, d.getTime() + fileEntry.name, function(s) {
//Callback with the new URL of the file.
callback(s.nativeURL);
}, function(error) {
alert('Error on moving file!');
callback(src); //Fallback, use the src given
});
}, function(error) {
alert('Error on getting parent!');
callback(src); //Fallback
});
}, function(error) {
alert('Error on resolveLocalFileSystemURI!');
callback(src); //Fallback
});
}
src
是 imageURI(即文件的路径(,callback
是上传文件的函数。(我应该注意,我们不完全确定是否需要getParent
调用,因为人们大概可以通过解析src
来获得DirectoryEntry
,但安全总比抱歉好(
注意:这需要文件插件,并且根据您的 Cordova 和 File 版本,可能需要进行一些编辑(因为 API 在版本之间发生了一些变化(。
相关文章:
- 使用Tabris-js,并尝试使用cordova文件和文件传输插件
- cordova图像上传出现文件传输错误
- 热以显示服务器间文件传输的进度条
- 服务器不读取科尔多瓦文件传输选项参数
- 文件传输在电话间隙代码401
- 图像文件因websocket文件传输而损坏
- 在 cordova 项目中,我正在使用文件传输插件上传个人资料照片,它总是返回代码 1 错误
- 科尔多瓦的文件传输写入错误(代码 1)
- 如何使用科尔多瓦文件传输下载 base 64 映像
- WebRTC文件传输和语法查询选择器
- 科尔多瓦多图像上传与文件传输
- Strophe.js文件传输的完整示例
- 为 v3.4.0 添加文件和文件传输科尔多瓦插件
- 电话间隙文件传输异步
- 仅从客户端检测下载开始/文件传输的开始
- 将facebook的响应从一个js文件传输到另一个文件的最佳方式
- 如何从Cordova文件传输插件中获取mimeType
- 文件传输正在进行中不起作用 - PhoneGap 3.5.0
- 节点文件传输在images目录中上载x字节的图像,但已损坏
- 使用WebSocket进行文件传输