[英]ng-file-upload function not triggering function
i'm trying to upload a file to my server like, save a path to database and file to folder, for that i tried ng-file-upload here is my code. 我正在尝试将文件上传到我的服务器,例如,保存数据库路径和文件到文件夹,为此,我尝试了ng-file-upload,这是我的代码。
<form ng-show="divPatient" name="PatientForm" ng-submit="AddUpdatePatient()">
<table>
<tr>
<td>
<input class="form-control" type="text" readonly placeholder="Key (Automatic)" ng-model="PatientID" />
</td>
<td>
<input id="FormFocus" name="FirstName" class="form-control" type="text" placeholder="FirstName" ng-model="FirstName" ng-minlength="3" required />
</td>
<td>
<input name="LastName" class="form-control" type="text" placeholder="LastName" ng-model="LastName" ng-minlength="3" required />
</td>
<td>
<input class="form-control" type="text" placeholder="Disease" ng-model="Disease" name="Disease" ng-minlength="3" required />
</td>
<td>
<input class="form-control" type="number" placeholder="Phone No." ng-model="PhoneNo" name="PhoneNo" ng-pattern="/^[789]\d{9}$/" required />
</td>
<td>
<input type="file" ng-file-select="onFileSelect($files)" class="form-control" ng-model="PhotoURL" required />
</td>
<td colspan="2">
<input type="submit" value="save" class="btn btn-success" ng-disabled="PatientForm.PhoneNo.$dirty && PatientForm.PhoneNo.$invalid || PatientForm.LastName.$dirty && PatientForm.LastName.$invalid || PatientForm.FirstName.$dirty && PatientForm.FirstName.$invalid || PatientForm.Disease.$dirty && PatientForm.Disease.$invalid" />
<input type="button" value="cancel" class="btn btn-primary" ng-click="CancelForm()" />
</td>
</tr>
<tr>
<td></td>
<td><span class="eror-text" ng-show="PatientForm.FirstName.$error.minlength">min 3 letters</span></td>
<td><span class="eror-text" ng-show="PatientForm.LastName.$error.minlength">min 3 letters</span></td>
<td><span class="eror-text" ng-show="PatientForm.Disease.$error.minlength">min 3 letters</span></td>
<td><span class="eror-text" ng-show="PatientForm.PhoneNo.$error.pattern">Invalid phone no</span></td>
</tr>
</table>
</form>
here is my Angular code 这是我的Angular代码
var app = angular.module('StudentApp', ['ngFileUpload']);
app.controller("StudentCtrl", function ($scope, angularService) {
$scope.selectedFile = [];
//On file Select
$scope.onFileSelect = function ($files) {
//This function not running at all ??
alert("where I'm i ???");
//$scope.selectedFile = $files;
//alert($files);
}
$scope.AddUpdatePatient = function () {
var file = $scope.selectedFile[0];
alert($scope.PhotoURL);
var Patient = {
FirstName: $scope.FirstName,
LastName: $scope.LastName,
Disease: $scope.Disease,
PhoneNo: $scope.PhoneNo
};
var getAction = $scope.Action;
if (getAction == "Update") {
Patient.PatientID = $scope.PatientID;
var getData = angularService.UpdatePatient(Patient,file);
getData.then(function (msg) {
GetAllPatients();
alert(msg.data);
$scope.divPatient = false;
}, function () {
alert('Error in updating record');
});
} else {
var getData = angularService.AddPatient(Patient);
getData.then(function (msg) {
GetAllPatients();
alert(msg.data);
$scope.divPatient = false;
}, function () {
alert('Error in adding record');
});
}
}
My dependencies 我的依赖
bundles.Add(new ScriptBundle("~/bundles/CustomPlugins").Include(
"~/Scripts/ng-file-upload-shim.min.js",
"~/scripts/angular.min.js",
"~/Scripts/ng-file-upload.min.js"));
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/CustomPlugins")
Where did i go wrong, pls tell me how do i upload file to the actionresult using angularjs, thank you 我在哪里出错了,请告诉我如何使用angularjs将文件上传到actionresult,谢谢
ok i tried onchange 好的,我尝试过onchange
onchange="angular.element(this).scope().selectFileforUpload(this.files)"
but i cant send that file to my action result, its showing me null, 但我无法将该文件发送给我的操作结果,显示为空,
$scope.selectFileforUpload = function (files) {
$scope.selectedFile = files;
}
$scope.AddUpdatePatient = function () {
var file = $scope.selectedFile[0];
var Patient = {
FirstName: $scope.FirstName,
LastName: $scope.LastName,
Disease: $scope.Disease,
PhoneNo: $scope.PhoneNo
};
var getData = angularService.AddPatient(Patient,filee);
getData.then(function (msg) {
GetAllPatients();
alert(msg.data);
$scope.divPatient = false;
}, function () {
alert('Error in adding record');
});
}
here is my service code 这是我的服务代码
this.AddPatient = function (patient, file) {
// this showing me file object thats fine
alert("in services" + filee);
// i think something wrong here we shouldn't or can't send the file to actionresult like this
var response = $http({
method: "post",
url: "AddPatient",
data: JSON.stringify(patient),
dataType: "json",
file : file
});
return response;
}
amd last my action result 最后我的行动结果
public string AddPatient(Patient patient,HttpPostedFileBase file)
{
// I'm getting patient data that is fine, but file is showing me null
}
is anything wrong with my service method ??? 我的服务方法有什么问题吗? or do you know how to send form along with files to actionresult or any method in the mvc controller, i heard some pleople do it by using "new FormData", but i dont know that, but i'm willing to learn it or use it, if that solves my problem
还是您知道如何将表格和文件一起发送到actionresult或mvc控制器中的任何方法,我听到有人通过使用“ new FormData”来做到这一点,但是我不知道,但是我愿意学习或使用它,如果那解决了我的问题
As Guinn mentions in the comments above, the 'ng' that is used for ng-file-upload is not the one in your code. 正如Guinn在上面的评论中提到的,用于ng-file-upload的'ng'不是您的代码中的那个。
The symptoms you are seeing definitely match what I have seen happen when the wrong directive is used. 当使用错误的指令时,您看到的症状与我所看到的完全匹配。
The ng-file-upload git-hb now actually contains some fiddles which you can verify in your browser and then use the same syntax to ensure your implementation works: ng-file-upload git-hb现在实际上包含一些小提琴,您可以在浏览器中对其进行验证,然后使用相同的语法来确保您的实现有效:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.