我试图借助Rest API在服务器和App之间传递数据。还需要对Rest API链接进行解码,而这涉及到encodeURIComponent来解码Rest API链接。 我得到的结果确实很奇怪,下载后图像变得损坏了。
我的临时服务器的链接。 http://freaksearch.com/aarti/rest-api.php?json=image&Id=
这是我使用encodeURIComponent的方式:
encodeURI('http://freaksearch.com/aarti/rest-api.php?json='+ encodeURIComponent('image&Id')+ imageId),
$scope.download = function(imageId, imageHeading) {
$ionicLoading.show({
template: 'Downloading...'
});
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
fs.root.getDirectory(
"MyProject",
{
create: true
},
function (dirEntry) {
dirEntry.getFile(
imageHeading + ".jpg",
{
create: true,
exclusive: false
},
function gotFileEntry(fe) {
var p = fe.toURL();
fe.remove();
ft = new FileTransfer();
ft.download(
encodeURI ('http://freaksearch.com/aarti/rest-api.php?json=' + encodeURIComponent('image&Id=') + imageId),
p,
function (entry) {
$ionicLoading.hide();
$scope.imgFile = entry.toURL();
},
function (error) {
$ionicLoading.hide();
alert("Download Error Source --> " + error.source);
},
false,
null
);
},
function () {
$ionicLoading.hide();
console.log("Get the file failed");
}
);
}
);
},
function () {
$ionicLoading.hide();
console.log("Request for filesystem failed");
});
}
$scope.load = function(imageId, imageHeading) {
$ionicLoading.show({
template: 'Loading...'
});
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
fs.root.getDirectory(
"ExampleProject",
{
create: false
},
function(dirEntry) {
dirEntry.getFile(
imageHeading + ".jpg",
{
create: false,
exclusive: false
},
function gotFileEntry(fe) {
$ionicLoading.hide();
$scope.imgFile = fe.toURL();
},
function(error) {
$ionicLoading.hide();
console.log("Error getting file");
}
);
}
);
},
function() {
$ionicLoading.hide();
console.log("Error requesting filesystem");
});
}