Dev*_*xon 32 javascript angularjs angularjs-http angularjs-fileupload
我正在尝试使用ng-upload在AngularJS中上传文件,但我遇到了问题.我的HTML看起来像这样:
<div class="create-article" ng-controller="PostCreateCtrl">
<form ng-upload method="post" enctype="multipart/form-data" action="/write" >
<fieldset>
<label>Category</label>
<select name="category_id" class="">
<option value="0">Select A Category</option>
<?php foreach($categories as $category): ?>
<option value="<?= $category -> category_id; ?>"><?= $category -> category_name; ?></option>
<?php endforeach; ?>
</select>
<label>Title</label>
<input type="text" class="title span5" name="post_title"
placeholder="A catchy title here..."
value="<?= $post -> post_title; ?>" />
<label>Attach Image</label>
<input type="file" name="post_image" />
<a href='javascript:void(0)' class="upload-submit: uploadPostImage(contents, completed)" >Crop Image</a>
<label>Body</label>
<div id="container">
<textarea id="mytextarea" wrap="off" name="post_content" class="span7" placeholder="Content..."><?= $post -> post_content; ?></textarea>
</div>
<div style='clear:both;'></div>
<label>Preview</label>
<div id='textarea-preview'></div>
</fieldset>
<div class="span7" style="margin: 0;">
<input type="submit" class="btn btn-success" value="Create Post" />
<input type="submit" class="btn btn-warning pull-right draft" value="Save as Draft" />
</div>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
我的js控制器看起来像这样:
ClabborApp.controller("PostCreateCtrl", ['$scope', 'PostModel',
function($scope, PostModel) {
$scope.uploadPostImage = function(contents, completed) {
console.log(completed);
alert(contents);
}
}]);
Run Code Online (Sandbox Code Playgroud)
我面临的问题是当裁剪图像被执行并执行uploadPostImage时,它会上传整个表单.不希望的行为,但我可以使它工作.最大的问题是在js中函数uploadPostImage'contents'参数总是未定义的,即使'completed'参数返回true也是如此.
目标是仅上传图像以进行裁剪.我在这个过程中做错了什么?
Fab*_*nte 61
上传文件的角度很少 - 没有文档.许多解决方案需要自定义指令其他依赖项(在primis中jquery ...只是为了上传文件...).经过多次尝试,我发现这只是angularjs(在v.1.0.6上测试过)
HTML
<input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this.files)"/>
Run Code Online (Sandbox Code Playgroud)
Angularjs(1.0.6)不支持"输入文件"标签上的ng-model,因此您必须以"本地方式"执行此操作,以便从用户传递所有(最终)选定的文件.
调节器
$scope.uploadFile = function(files) {
var fd = new FormData();
//Take the first selected file
fd.append("file", files[0]);
$http.post(uploadUrl, fd, {
withCredentials: true,
headers: {'Content-Type': undefined },
transformRequest: angular.identity
}).success( ...all right!... ).error( ..damn!... );
};
Run Code Online (Sandbox Code Playgroud)
很酷的部分是未定义的内容类型和transformRequest:angular.identity,它们在$ http处为选择正确的"内容类型"并管理处理多部分数据时所需的边界.
dan*_*ial 19
你可以试试ng-file-upload angularjs插件(而不是ng-upload).
设置和处理angularjs细节相当容易.它还支持进度,取消,拖放和跨浏览器.
HTML
<!-- Note: MUST BE PLACED BEFORE angular.js-->
<script src="ng-file-upload-shim.min.js"></script>
<script src="angular.min.js"></script>
<script src="ng-file-upload.min.js"></script>
<div ng-controller="MyCtrl">
<input type="file" ngf-select="onFileSelect($files)" multiple>
</div>
Run Code Online (Sandbox Code Playgroud)
JS:
//inject angular file upload directives and service.
angular.module('myApp', ['ngFileUpload']);
var MyCtrl = [ '$scope', '$upload', function($scope, $upload) {
$scope.onFileSelect = function($files) {
//$files: an array of files selected, each file has name, size, and type.
for (var i = 0; i < $files.length; i++) {
var file = $files[i];
$scope.upload = $upload.upload({
url: 'server/upload/url', //upload.php script, node.js route, or servlet url
data: {myObj: $scope.myModelObj},
file: file,
}).progress(function(evt) {
console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
}).then(function(response) {
var data = response.data;
// file is uploaded successfully
console.log(data);
});
}
};
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
119602 次 |
| 最近记录: |