angularjs文件上载在使用php选择文件时
我想上传一个没有提交按钮的选择后的图像。 我使用了GitHub的插件。 我想他用C制作了服务器,我想把它改成php。我在编写php文件处理程序时遇到问题 这是我的脚本文件angularjs文件上载在使用php选择文件时,php,angularjs,ng-file-upload,Php,Angularjs,Ng File Upload,我想上传一个没有提交按钮的选择后的图像。 我使用了GitHub的插件。 我想他用C制作了服务器,我想把它改成php。我在编写php文件处理程序时遇到问题 这是我的脚本文件 var app = angular.module('fileUpload', ['ngFileUpload']); app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
var app = angular.module('fileUpload', ['ngFileUpload']);
app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
$scope.uploadPic = function(file) {
file.upload = Upload.upload({
method:'POST',
url: 'http://localhost/angular/upload.php',
data: {file: file, username: $scope.username}
});
file.upload.then(function (response) {
$timeout(function () {
file.result = response.data;
});
}, function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
}, function (evt) {
file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
});
}
}]);
这是我的html文件
<form name="myForm" >
<fieldset>
<legend>Upload on file select</legend>
<br>Photo:
<input type="file" ngf-select="uploadPic(picFile)" ng-model="picFile" name="picFile"
accept="image/*" ngf-max-size="2MB" required
ngf-model-invalid="errorFiles">
<img ng-show="myForm.file.$valid" ngf-thumbnail="picFile" class="thumb">
<br>
<button ng-disabled="!myForm.$valid"
ng-click="uploadPic(picFile)">Submit</button>
<div class="con">
<div class="pg" style="width:{{picFile.progress}}%" ></p></div>
</div>
<p ng-bind="picFile.progress + '%'"></p>
<span ng-show="picFile.result">Upload Successful</span>
<span class="err" ng-show="errorMsg">{{errorMsg}}</span>
</fieldset>
</form>
我的xampp服务器正在工作。我还包括angularjs、ng-file-upload-shim.min.js和ng-file-upload.min.js等angular文件。我找到了这个问题的答案,问题不在代码上,问题的发生是因为我将XMLHttpRequest设置到了一个不同的域。解决这个问题的简单方法是在chrome中添加扩展。这解决了问题。什么不起作用?是404吗?500美元?服务返回什么,是否有语法错误等方面的输出?图像未上载。@danial我将html输入文件从ngf select=UPLOADPICFILE更改为ngf select uploadPic$文件,但仍不工作。$file是否应该类似于输入的ng模型。输入的名称是什么。也应该类似。尝试在上传模块repo中使用php代码
<style>
.thumb {
width: 240px;
height: 240px;
float: none;
position: relative;
top: 7px;
}
form .progress {
line-height: 1px;
}
.progress {
display: inline-block;
width: 300px;
}
.con{
width:300px;
}
.pg {
font-size: smaller;
background: #000000;
width:0px;
height:1px;
}
</style>
<?php if ( !empty( $_FILES ) ) {
$tempPath = $_FILES[ 'picFile' ][ 'tmp_name' ];
$uploadPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $_FILES[ 'picFile' ][ 'name' ];
move_uploaded_file( $tempPath, $uploadPath );
$answer = array( 'answer' => 'File transfer completed' );
$json = json_encode( $answer );
echo $json; } else {
echo 'No files'; } ?>