ngcordova android文件选择并上传到服务器
我想将文件从ionic应用程序上载到服务器。我正在使用cordovafiletransfer插件。使用它,我可以通过在控制器代码中提供静态路径来上传文件。我的问题是如何获得用户选择的文件路径?我只从所选文件的相对路径上的输入标记获取文件名。怎么弄到的 查看页面代码:ngcordova android文件选择并上传到服务器,android,angularjs,cordova,ionic-framework,Android,Angularjs,Cordova,Ionic Framework,我想将文件从ionic应用程序上载到服务器。我正在使用cordovafiletransfer插件。使用它,我可以通过在控制器代码中提供静态路径来上传文件。我的问题是如何获得用户选择的文件路径?我只从所选文件的相对路径上的输入标记获取文件名。怎么弄到的 查看页面代码: <label class="item item-input"> <div class="input-label">Upload Photo</div> <input type
<label class="item item-input">
<div class="input-label">Upload Photo</div>
<input type="file" onchange="angular.element(this).scope().setFile(this)" accept="image/*"/>
</label>
<div class="padding">
<button ng-click="upload()" class="button button-block button-assertive">Upload</button>
</div>
如何获得所选文件的正确目标路径?我遇到了解决问题的方法(它对我有效,但需要一些修改):
在我的模板中:
请记住在上载文档后进行清理
使用:
另请参阅,以了解有关
文件阅读器的更多信息。您是否找到了解决方案?
$scope.upload = function() {
var filename, options, targetPath;
console.log($scope.theFile);
targetPath = cordova.file.externalRootDirectory + '/Download/androidify.png';
filename = targetPath.split('/').pop();
options = {};
options.fileKey = 'image_file';
options.fileName = $scope.theFile.name;
options.chunkedMode = false;
options.mimeType = $scope.theFile.type;
options.headers = {
'Authorization': getDeviceToken()
};
console.log(options);
return $cordovaFileTransfer.upload(domain.uploadphoto(), targetPath, options).then(
(function(result) {
console.log('SUCCESS: ' + JSON.stringify(result.response));
}),
(function(err) {
console.log('ERROR: ' + JSON.stringify(err));
}),
function(progress) {}
);
};
$scope.setFile = function(element) {
return $scope.$apply(function($scope) {
console.log(element);
return $scope.theFile = element.files[0];
});
};
<input type="file" fileread="file.path" />
<button type="button" class="button button-small button-assertive" ng-click="vm.upload(file)">Upload PDF</button>
.directive("fileread", ['$cordovaFile', function ($cordovaFile) {
return {
scope: {
fileread: "="
},
link: function (scope, element, attributes) {
element.bind("change", function (changeEvent) {
var reader = new FileReader();
reader.onload = function (loadEvent) {
var fileName = changeEvent.target.files[0].name;
$cordovaFile.writeFile(cordova.file.dataDirectory, fileName, loadEvent.target.result, true)
.then(function (result) {
console.log(result)
}, function (err) {
console.warn(err)
});
scope.$apply(function () {
scope.fileread = cordova.file.dataDirectory + fileName;
});
}
reader.readAsArrayBuffer(changeEvent.target.files[0]);
});
}
}
}]);
$cordovaFile.removeFile(cordova.file.dataDirectory, fileName)
.then(function (success) {
// success
}, function (error) {
// error
});