如何要求用户上传文件并在我的代码中使用它
How to ask the user to upload a file and use it in my code
嘿伙计们,我想知道如何将一个 json 替换为用户上传的另一个 json。
这是我的JavaScript代码
var app = angular.module('miApp', []);
app.controller('mainController', function ($scope, $http) {
$http.get('cuidades.json').success(function(data){
$scope.cities = data;
});
});
这是我的 html 代码,使用此代码我可以从我的计算机中获取一个 json,它很好,但我不知道如何要求用户上传文件并加载 HIS 文件。
<!doctype html>
<html ng-app="miApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js">
</script>
<script src="prueba.js"></script>
</head>
<body ng-controller="mainController">
<h1>Ciudades</h1>
<table>
<label>Filtro:</label>
<input ng-model="query">
<hr>
<tr ng-repeat="city in cities | filter:query | orderBy:'name'">
<td> {{city.name}} - {{city.country}}</td>
</tr>
</table>
<label>Selecciona un archivo para subir</label>
<br />
<input type="file" ng-model-instant/>
<input type="submit" value='submit'/>
</body>
</html>
正如布莱恩所说,你可以使用 https://github.com/danialfarid/ng-file-upload。这是我的代码,基于我在谷歌上搜索的一个示例。
$scope.$watch('foto', function () {
$scope.uploadFile($scope.foto);
});
$scope.uploadFile = function () {
var file;
if ($scope.foto) {
file = $scope.foto[0];
}
if (!file) return;
console.log('uploadfile()');
console.log('file', file);
$scope.upload = Upload.upload({
url: '/api/photo',
method: 'POST',
data: angular.toJson($rootScope.usuario),
file: file
}).progress(function (evt) {
$scope.uploadProgress = parseInt(100.0 * evt.loaded / evt.total, 10);
console.log('uploadProgress', $scope.uploadProgress);
}).success(function (data) {
console.log('Sucesso no upload');
console.log('data', data);
$rootScope.usuario.foto = '/uploads/' + data.files.file.name;
});
};
还有我的 html 代码:
<input type="file" name="userPhoto" ngf-select ng-model="foto" />
<div class="progress" style="margin-top: 20px;">
<div class="progress-bar" progress-bar="uploadProgress" role="progressbar">
<span ng-bind="uploadProgress"></span>
<span>%</span>
</div>
</div>
希望我有帮助
相关文章:
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 从我的控制器返回一个不同于200的代码以触发ajax错误,这被认为是一种好的做法吗
- 验证我的代码json
- 如何创建更好的方式来维护基于我的代码访问的最后一个页面
- 我的jquery代码不起作用.为什么?
- 为什么不是'我的函数在解析云代码中工作吗?当我在Angular和Express中测试时,它是有效的
- 我不知道为什么我的代码是错误的?又有什么错
- 为什么我的解析云代码请求'未经授权'
- 为什么不't我的ruby代码与javascript文件一起插入
- 我的html表单无法验证.请参阅代码片段中的html代码和java脚本
- 我需要一个jQuery函数来只工作在700px以上的屏幕大小,无法在我的代码中发现错误
- I'我不知道为什么我的代码没有'不起作用
- 如何使用我的Jquery代码创建委托事件侦听器
- 我不知道此代码中的这些符号是什么意思.十进制到二进制
- 什么正在取代我的'以及“;javascript代码中使用&#39;和&”;
- 在上传之前,我可以在代码中的哪里将文件扩展名更改为小写
- 为什么动态加载的事件在我的代码中不能正常工作
- 我的Javascript代码出错
- 如何禁用我的切换按钮?这是我的代码
- 如何分离代码:我的控制器是否做了太多工作