角度输入上传 - 获取文件名并上传
Angular Input Upload - Get file name and upload
我正在尝试为Ionic/Angular应用程序的用户配置文件更新部分创建图像上传功能。上传功能是表单的一部分,我无法检索图像和文件名。我如何获得这两件物品?下面是我的代码:
表单(视图(:
<div class="item-input">
<!--list item-->
<div data-ng-repeat="user in users">
Username: <input type="text" placeholder="Enter the event name" name="username" ng-model="user.username" required>
Password: <input type="password" placeholder="Enter the password" name="password" ng-model="user.password" required>
Email: <input type="text" placeholder="Enter the email" name="email" ng-model="user.email" required>
Hometown: <input type="text" placeholder="Enter your hometown" name="hometown" ng-model="user.hometown" required>
Firstname: <input type="text" name="firstname" ng-model="user.firstname" required>
Lastname: <input type="text" name="lastname" ng-model="user.lastname" required>
Birthday: <date-picker ng-model="user.birthday"></date-picker>
Image: <input type="file" name="file" ng-model="filename">
<button ng-click="upload(file)">Upload</button>
<button class="button button-block button-positive" ng-click="editprofile(user)">
Edit Account
</button>
<button class="button button-block button-positive" ng-click="deleteprofile()">
Delete Account
</button>
</div>
控制器
.controller('ProfileUpdateCtrl', function($http, $state, $http, $cordovaOauth, $stateParams, $rootScope, $scope, UserFac) {
//removed the working features to focus on the uploading part.
$scope.upload = function(file) {
var filename = $scope.file.name;
//need to know how to get the data of the image and save as formdata
}
});
我已经使用angularjs创建了一个图像上传示例。它检索图像及其文件名。我希望它能帮助你。
.HTML:
<div ng-app="test">
<div ng-controller="UploadCtrl">
Image:
<input type="file" name="file" onchange="angular.element(this).scope().photoChanged(this.files)" />
<img ng-src="{{ thumbnail.dataUrl }}" /> <!--Display the image -->
</div>
控制器:
angular.module('test', []);
angular.module('test') .controller('UploadCtrl', function($scope, $timeout) {
// Read the image using the file reader
$scope.fileReaderSupported = window.FileReader != null;
$scope.photoChanged = function(files) {
if (files != null) {
var file = files[0];
if ($scope.fileReaderSupported && file.type.indexOf('image') > -1) {
$timeout(function() {
var fileReader = new FileReader();
fileReader.readAsDataURL(file); // convert the image to data url.
fileReader.onload = function(e) {
$timeout(function() {
$scope.thumbnail.dataUrl = e.target.result; // Retrieve the image.
});
}
});
}
}
};
});
JS小提琴:https://jsfiddle.net/um8p6pd7/2/
祝你好运!
您需要
使用 $cordovaFileTransfer 将文件上传到服务器。安装 $cordovaFileTransfer 插件,然后您就可以将文件上传到服务器,在服务器端您需要创建获取文件的路径并将其保存到文件夹中。
相关文章:
- JS-使用Ajax获取文件名
- 如何在JavaScript中基于事件对象获取文件名
- 正在获取客户端计算机上特定目录中的文件名
- 从Javascript中的输入文件中获取文件名
- 如何从 URL 中获取文件名和扩展名
- 通过jQuery获取上传的文件名时遇到问题
- 从 PhotoPrevs.append 获取文件名
- 上传前获取图片的文件名-JQuery
- 从javascript中的字符串路径获取文件名
- 谷歌表格-从硬盘获取文件名并插入表格-(模拟Excel's Appification.GetOpenFilena
- 用于获取<输入类型=文件>只返回firefox中的文件名
- 从url获取文件名
- 如何获取在 Node 中调用函数的文件名和行号
- 如何在 JavaScript 中获取对象的文件名
- 使用 Phonegap 文件 API 获取所有文件名
- 如何从IE浏览器中的输入文件标签中获取javascript中的文件名
- 无法在节点应用程序上获取/文件名错误
- 从远程服务器目录获取文件名列表
- 通过输入文件名获取目录路径
- 使用 PHP 或 JavaScript 将文件夹中的文件名获取到页面中