[英]ng-file-upload function not triggering function
我正在嘗試將文件上傳到我的服務器,例如,保存數據庫路徑和文件到文件夾,為此,我嘗試了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>
這是我的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');
});
}
}
我的依賴
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")
我在哪里出錯了,請告訴我如何使用angularjs將文件上傳到actionresult,謝謝
好的,我嘗試過onchange
onchange="angular.element(this).scope().selectFileforUpload(this.files)"
但我無法將該文件發送給我的操作結果,顯示為空,
$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');
});
}
這是我的服務代碼
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;
}
最后我的行動結果
public string AddPatient(Patient patient,HttpPostedFileBase file)
{
// I'm getting patient data that is fine, but file is showing me null
}
我的服務方法有什么問題嗎? 還是您知道如何將表格和文件一起發送到actionresult或mvc控制器中的任何方法,我聽到有人通過使用“ new FormData”來做到這一點,但是我不知道,但是我願意學習或使用它,如果那解決了我的問題
正如Guinn在上面的評論中提到的,用於ng-file-upload的'ng'不是您的代碼中的那個。
當使用錯誤的指令時,您看到的症狀與我所看到的完全匹配。
ng-file-upload git-hb現在實際上包含一些小提琴,您可以在瀏覽器中對其進行驗證,然后使用相同的語法來確保您的實現有效:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.