在ionicframwork中获取所选文件的来源

时间:2017-03-29 23:34:57

标签: angularjs cordova ionic-framework

我正在使用离子构建移动应用。其中一个用例是让用户浏览文件并将其上传到后端服务器(公开休息服务)。 在UI上,我使用的是html文件标记。

<input type="file" ng-select="uploadFile($files)" multiple>

.controller('UploadDocCtrl', function ($scope, $cordovaFileTransfer) {

  $scope.uploadFile = function(files) {
   console.log("selected file "+files);
   // hard coded file path "/android_asset/www/img/ionic.pdf" to be replaced with the user selected file
   $cordovaFileTransfer.upload(restServiceEndpoint, "/android_asset/www/img/ionic.pdf", properties).then(function(result) {
   console.log("SUCCESS: " + JSON.stringify(result.response));
  }, function(err) {
        console.log("ERROR: " + JSON.stringify(err));
   }, function (progress) {
// constant progress updates
  });
});

问题是我无法获得对所选文件的引用。有人可以请帮助实现这一目标的步骤。谢谢!

1 个答案:

答案 0 :(得分:0)

将文件保存在具有index.html文件的www文件夹中。

然后给出像&#34; ionic.pdf&#34;

.controller('UploadDocCtrl', function ($scope, $cordovaFileTransfer) {

  $scope.uploadFile = function(files) {
   console.log("selected file "+files);
   // hard coded file path "/android_asset/www/img/ionic.pdf" to be replaced with the user selected file
   $cordovaFileTransfer.upload(restServiceEndpoint, "ionic.pdf", properties).then(function(result) {
   console.log("SUCCESS: " + JSON.stringify(result.response));
  }, function(err) {
        console.log("ERROR: " + JSON.stringify(err));
   }, function (progress) {
// constant progress updates
  });
});