我想從我的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)
{ }
}}
謝謝你的回答...我試着用你的解決方案... 我認爲我幾乎在那裏..我現在面臨的問題是,我能夠在後端看到一個Form值爲「Employee」。 。但是如何獲取$ scope.Employee中的值。 其實我是一個初學者,所以我的問題可以是簡單的或沒有意義的。 –
你可以請調試,並檢查裏面哪個值,請給我這裏,所以我可以給你更好的解決方案 –
謝謝你的幫助.. 。我添加了一些循環,並在每個關鍵字的後端獲取了值 –