ng-file-upload函数不触发函数

时间:2015-07-08 07:13:03

标签: javascript asp.net-mvc angularjs angular-file-upload

我正在尝试将文件上传到我的服务器,比如保存数据库和文件到文件夹的路径,因为我尝试过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>

这是我的角色代码

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)"

但是我无法将该文件发送到我的操作结果,它显示为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');
        });
}

这是我的服务代码

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
    }

我的服务方法有什么问题???或者你知道如何将文件与文件一起发送到actionresult或mvc控制器中的任何方法,我听说有些人通过使用“新FormData”来做,但我不知道,但我愿意学习或使用它,如果这解决了我的问题

1 个答案:

答案 0 :(得分:0)

正如Guinn在上面的评论中提到的那样,&#39; ng&#39;用于ng-file-upload的不是代码中的那个。

您所看到的症状与使用错误指令时发生的症状完全一致。

ng-file-upload git-hb现在实际上包含一些你可以在浏览器中验证的小提琴,然后使用相同的语法来确保你的实现有效: