簡體   English   中英

在上傳包含一些數據的文件時,在AngularJS中使用$ http.post發送兩個對象

[英]Send two objects with $http.post in AngularJS while uploading a file with some data

我想將請求從我的js文件發送到我的控制器中。

我在做什么

var fd = new FormData();
    for (var i = 0; i < file.length; i++) {
        fd.append("file", file[i]);
    }

$http.post('Employees/', $scope.Employee, fd, {
        transformRequest: angular.identity,
        headers: { 'Content-Type': undefined }
    }).success(function (data) {
        $scope.AddEditEmployee = false;
        getEmployees();
    }).error(function (data, status, headers, config) {
    });

我在這里面臨的問題是我得到了$ scope.Employees的值,但fd為空。 這是在AngularJS中上傳帶有一些數據的文件的一部分

我控制器中的代碼是

    public class EmployeesController: ApiController
{
    EmployeeDM objEmpManager = new EmployeeDM(BaseDapper.CreateInstance());
[Route("Employees/")]
    [HttpPost]
    public void Post(EmployeeBO obj_emp)
    //public void Post(EmployeeBO obj_emp1, EmployeeBO obj_emp)
    {
        try
        {
            var httpRequest = HttpContext.Current.Request;
            string image_names = string.Empty;
            string ResumeName = string.Empty;
            string resumePath = HttpContext.Current.Server.MapPath("/Uploads/Resume/");
            if (httpRequest.Files.Count > 0)
            {
                string resume_full_path = string.Empty;
                for (int i = 0; i < httpRequest.Files.Count; i++)
                {
                    var postedFile = httpRequest.Files[i];
                    ResumeName = postedFile.FileName;
                    resume_full_path = resumePath + obj_emp.name + ResumeName;
                    postedFile.SaveAs(resume_full_path);
                    image_names += resume_full_path.ToString() + ',';
                    obj_emp.resume = resume_full_path;
                    if (obj_emp.employee_id > 0)
                    {
                        objEmpManager.UpdateEmployee(obj_emp);
                    }
                    else
                    {
                        objEmpManager.SaveEmployee(obj_emp);
                    }
                }
            }

        }
        catch (Exception ex)
        { }

    }}

嘗試這個:

$http({
method:"POST",
url:"Employees/",
transformRequest: angular.identity,
headers: { 'Content-Type': undefined },
data:{obj_emp1:$scope.Employee , obj_emp:fd }
}).then(
function success(data){
$scope.AddEditEmployee = false;
getEmployees();}, 
function error(data){
});

嘗試這個。

var fd = new FormData();
for (var i = 0; i < file.length; i++) {
    fd.append("file", file[i]);
}

for (var key in $scope.Employee) {
        fd.append(key, $scope.Employee[key]);
        alert($scope.Employee[key]);
    }

$http.post('Employees/', fd, {
        headers: { 'Content-Type': undefined }
    }).success(function (data) {
        $scope.AddEditEmployee = false;
        getEmployees();
    }).error(function (data, status, headers, config) {
    });

從后端,您需要刪除對象作為參數,然后從請求形式中找到它。

    public void Post(){ 
EmployeeBo obj = new EmployeeBo();
obj.key= HttpContext.Current.Request.Form["Key"];

.........
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM