我正在尝试将图像路径数据库和图像本身保存到项目中的文件夹中
I am trying to save Image path database and the image itslef to a folder in project
我在网络上工作 api 并且我是 API 的新手,因此我真的很难跟上我的项目,因为我创建了两个项目,其中一个在前面- end 和另一个有后端,我想在保存它的数据库路径后保存我的图像的文件夹在我的前端项目中。
这是我在我的前端项目中编写的 ajax 这个 ajax 击中了我的后端项目中的控制器:
$('.empOfficialDetails').click(function (ev) {
ev.preventDefault();
var data = new Object();
data.UserName = $('#username').val();
data.UPassword = $('#userpass').val();
data.OfficialEmailAddress = $('#officialemail').val();
data.Departments = $('#departments :selected').text();
data.Designation = $('#designation :selected').text();
data.RoleID = $('#role').val();
data.Role = $('#role :selected').text();
data.ReportToID = $('#reportToID').val();
data.ReportTo = $('#reportTo :selected').text();
data.JoiningDate = $('#joindate').val();
data.IsAdmin = $('#isAdmin :selected').val() ? 1 : 0;
data.IsActive = $('#isActive :selected').val() ? 1 : 0;
data.IsPermanent = $('#isPermanent :selected').val() ? 1 : 0;
data.DateofPermanancy = $('#permanantdate').val();
data.HiredbyReference = $('#hiredbyRef :selected').val() ? 1 : 0;
data.HiredbyReferenceName = $('#refePersonName').val();
data.BasicSalary = $('#basicSalary').val();
data.CurrentPicURL = $('.picture').val();
if (data.UserName && data.UPassword && data.OfficialEmailAddress && data.Departments && data.Designation && data.Role && data.IsAdmin && data.IsPermanent) {
$.ajax({
url: 'http://localhost:1089/api/Employee/EmpOfficialDetails',
type: "POST",
dataType: 'json',
contentType: "application/json",
data: JSON.stringify(data),
beforeSend: function () {
$("#dvRoomsLoader").show();
},
complete: function () {
$("#dvRoomsLoader").hide();
},
success: function (data) {
var ID = parseInt(data);
if (ID > 0) {
//var id = data;
$(".HiddenID").val(data);
//var id = $(".HiddenID").val();
$('#official').css('display', 'block');
$('#official').html("Employees Official details added successfully...!");
$('#official').fadeOut(25000);
$("#dvRoomsLoader").show();
$('.empOfficialDetails').html("Update <i class='fa fa-angle-right rotate-icon'></i>");
}
else {
$('#official').find("alert alert-success").addClass("alert alert-danger").remove("alert alert-success");
}
},
error: function (ex) {
alert("There was an error while submitting employee data");
alert('Error' + ex.responseXML);
alert('Error' + ex.responseText);
alert('Error' + ex.responseJSON);
alert('Error' + ex.readyState);
alert('Error' + ex.statusText);
}
});
}
return false;
});
请告诉我我应该以什么形式发送来自 ajax 的图像,以及目前我的 Current PicUrl 仅作为字符串传递并且在数据库中它看起来像这样 C:\fakepath\access-matrix-design.PNG,我希望整个图像通过 ajax 并与其路径一起保存在数据库中
这是我的后端项目中控制器中的方法:
public int Emp_OfficialDetails(Employee emp)
{
//SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AmanraHRMS"].ConnectionString);
var con = DB.getDatabaseConnection();
SqlCommand com = new SqlCommand("sp_InsEmpOfficialDetails", con);
com.CommandType = CommandType.StoredProcedure;
#region Employee Official Details Insert Code block
com.Parameters.AddWithValue("@UserName", emp.UserName);
com.Parameters.AddWithValue("@pass", emp.UPassword);
com.Parameters.AddWithValue("@OfficialEmailAddress", emp.OfficialEmailAddress);
com.Parameters.AddWithValue("@Department", emp.Departments);
com.Parameters.AddWithValue("@Role", emp.Role);
com.Parameters.AddWithValue("@IsAdmin", Convert.ToBoolean(emp.IsAdmin));
com.Parameters.AddWithValue("@Designation", emp.Designation);
com.Parameters.AddWithValue("@ReportToID", emp.ReportToID);
com.Parameters.AddWithValue("@ReportTo", emp.ReportTo);
com.Parameters.AddWithValue("@JoiningDate", Convert.ToDateTime(emp.JoiningDate));
com.Parameters.AddWithValue("@IsPermanent", Convert.ToBoolean(emp.IsPermanent));
com.Parameters.AddWithValue("@DateofPermanancy", Convert.ToDateTime(emp.DateofPermanancy));
com.Parameters.AddWithValue("@IsActive", Convert.ToBoolean(emp.IsActive));
com.Parameters.AddWithValue("@HiredbyReference", Convert.ToBoolean(emp.HiredbyReference));
com.Parameters.AddWithValue("@HiredbyReferenceName", emp.HiredbyReferenceName);
com.Parameters.AddWithValue("@BasicSalary", emp.BasicSalary);
com.Parameters.AddWithValue("@CurrentPicURL", emp.CurrentPicURL);
#endregion
EmployeeImage(emp, emp.CurrentPicURL);
var ID = com.ExecuteScalar();
com.Clone();
return Convert.ToInt32(ID);
}
//Ajax call hit this method from AddEmployee page
[Route("api/Employee/EmpOfficialDetails")]
[HttpPost]
public int? EmpOfficialDetails(Employee emp)
{
IHttpActionResult ret;
try
{
var id = Emp_OfficialDetails(emp);
return id;
}
catch (Exception ex)
{
ret = InternalServerError(ex);
}
return null;
}
如您所见,我正在调用我创建的 EmployeeImage 来保存和处理图像 ,但我不知道如何传递参数由于 HttpPostedFileBase,所以使用此方法,这是我处理图像和保存图像路径的方法:
public void EmployeeImage(Employee emp, HttpPostedFileBase CurrentPicURL)
{
var allowedExtensions = new[] { ".Jpg", ".png", ".jpg", "jpeg" };
var fileName = Path.GetFileName(CurrentPicURL.FileName);
var ext = Path.GetExtension(CurrentPicURL.FileName); //getting the extension(ex-.jpg)
byte[] bytes;
using (BinaryReader br = new BinaryReader(CurrentPicURL.InputStream))
{
bytes = br.ReadBytes(CurrentPicURL.ContentLength);
}
if (allowedExtensions.Contains(ext)) //check what type of extension
{
string name = Path.GetFileNameWithoutExtension(fileName); //getting file name without extension
string myfile = name + "_" + ext; //appending the name with id
var path = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/assets/img/profiles/employeeImages"), myfile); // store the file inside ~/project folder(Img)
CurrentPicURL.SaveAs(path);
}
}
请告诉我如何在我的 EmpDetails 方法中调用此方法并将我的 CurrentPicURL 作为参数传递给此方法,此路径 "~/assets/img/profiles/employeeImages"我指的是位于我的前端项目
非常感谢我得到的所有帮助
谢谢
$('.picture').val()
将为您提供文件路径和名称 (C:\fakepath\access-matrix-design.PNG
)
如果要获取文件对象,请使用$('.picture')[0].files[0]
我在网络上工作 api 并且我是 API 的新手,因此我真的很难跟上我的项目,因为我创建了两个项目,其中一个在前面- end 和另一个有后端,我想在保存它的数据库路径后保存我的图像的文件夹在我的前端项目中。
这是我在我的前端项目中编写的 ajax 这个 ajax 击中了我的后端项目中的控制器:
$('.empOfficialDetails').click(function (ev) {
ev.preventDefault();
var data = new Object();
data.UserName = $('#username').val();
data.UPassword = $('#userpass').val();
data.OfficialEmailAddress = $('#officialemail').val();
data.Departments = $('#departments :selected').text();
data.Designation = $('#designation :selected').text();
data.RoleID = $('#role').val();
data.Role = $('#role :selected').text();
data.ReportToID = $('#reportToID').val();
data.ReportTo = $('#reportTo :selected').text();
data.JoiningDate = $('#joindate').val();
data.IsAdmin = $('#isAdmin :selected').val() ? 1 : 0;
data.IsActive = $('#isActive :selected').val() ? 1 : 0;
data.IsPermanent = $('#isPermanent :selected').val() ? 1 : 0;
data.DateofPermanancy = $('#permanantdate').val();
data.HiredbyReference = $('#hiredbyRef :selected').val() ? 1 : 0;
data.HiredbyReferenceName = $('#refePersonName').val();
data.BasicSalary = $('#basicSalary').val();
data.CurrentPicURL = $('.picture').val();
if (data.UserName && data.UPassword && data.OfficialEmailAddress && data.Departments && data.Designation && data.Role && data.IsAdmin && data.IsPermanent) {
$.ajax({
url: 'http://localhost:1089/api/Employee/EmpOfficialDetails',
type: "POST",
dataType: 'json',
contentType: "application/json",
data: JSON.stringify(data),
beforeSend: function () {
$("#dvRoomsLoader").show();
},
complete: function () {
$("#dvRoomsLoader").hide();
},
success: function (data) {
var ID = parseInt(data);
if (ID > 0) {
//var id = data;
$(".HiddenID").val(data);
//var id = $(".HiddenID").val();
$('#official').css('display', 'block');
$('#official').html("Employees Official details added successfully...!");
$('#official').fadeOut(25000);
$("#dvRoomsLoader").show();
$('.empOfficialDetails').html("Update <i class='fa fa-angle-right rotate-icon'></i>");
}
else {
$('#official').find("alert alert-success").addClass("alert alert-danger").remove("alert alert-success");
}
},
error: function (ex) {
alert("There was an error while submitting employee data");
alert('Error' + ex.responseXML);
alert('Error' + ex.responseText);
alert('Error' + ex.responseJSON);
alert('Error' + ex.readyState);
alert('Error' + ex.statusText);
}
});
}
return false;
});
请告诉我我应该以什么形式发送来自 ajax 的图像,以及目前我的 Current PicUrl 仅作为字符串传递并且在数据库中它看起来像这样 C:\fakepath\access-matrix-design.PNG,我希望整个图像通过 ajax 并与其路径一起保存在数据库中
这是我的后端项目中控制器中的方法:
public int Emp_OfficialDetails(Employee emp)
{
//SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AmanraHRMS"].ConnectionString);
var con = DB.getDatabaseConnection();
SqlCommand com = new SqlCommand("sp_InsEmpOfficialDetails", con);
com.CommandType = CommandType.StoredProcedure;
#region Employee Official Details Insert Code block
com.Parameters.AddWithValue("@UserName", emp.UserName);
com.Parameters.AddWithValue("@pass", emp.UPassword);
com.Parameters.AddWithValue("@OfficialEmailAddress", emp.OfficialEmailAddress);
com.Parameters.AddWithValue("@Department", emp.Departments);
com.Parameters.AddWithValue("@Role", emp.Role);
com.Parameters.AddWithValue("@IsAdmin", Convert.ToBoolean(emp.IsAdmin));
com.Parameters.AddWithValue("@Designation", emp.Designation);
com.Parameters.AddWithValue("@ReportToID", emp.ReportToID);
com.Parameters.AddWithValue("@ReportTo", emp.ReportTo);
com.Parameters.AddWithValue("@JoiningDate", Convert.ToDateTime(emp.JoiningDate));
com.Parameters.AddWithValue("@IsPermanent", Convert.ToBoolean(emp.IsPermanent));
com.Parameters.AddWithValue("@DateofPermanancy", Convert.ToDateTime(emp.DateofPermanancy));
com.Parameters.AddWithValue("@IsActive", Convert.ToBoolean(emp.IsActive));
com.Parameters.AddWithValue("@HiredbyReference", Convert.ToBoolean(emp.HiredbyReference));
com.Parameters.AddWithValue("@HiredbyReferenceName", emp.HiredbyReferenceName);
com.Parameters.AddWithValue("@BasicSalary", emp.BasicSalary);
com.Parameters.AddWithValue("@CurrentPicURL", emp.CurrentPicURL);
#endregion
EmployeeImage(emp, emp.CurrentPicURL);
var ID = com.ExecuteScalar();
com.Clone();
return Convert.ToInt32(ID);
}
//Ajax call hit this method from AddEmployee page
[Route("api/Employee/EmpOfficialDetails")]
[HttpPost]
public int? EmpOfficialDetails(Employee emp)
{
IHttpActionResult ret;
try
{
var id = Emp_OfficialDetails(emp);
return id;
}
catch (Exception ex)
{
ret = InternalServerError(ex);
}
return null;
}
如您所见,我正在调用我创建的 EmployeeImage 来保存和处理图像 ,但我不知道如何传递参数由于 HttpPostedFileBase,所以使用此方法,这是我处理图像和保存图像路径的方法:
public void EmployeeImage(Employee emp, HttpPostedFileBase CurrentPicURL)
{
var allowedExtensions = new[] { ".Jpg", ".png", ".jpg", "jpeg" };
var fileName = Path.GetFileName(CurrentPicURL.FileName);
var ext = Path.GetExtension(CurrentPicURL.FileName); //getting the extension(ex-.jpg)
byte[] bytes;
using (BinaryReader br = new BinaryReader(CurrentPicURL.InputStream))
{
bytes = br.ReadBytes(CurrentPicURL.ContentLength);
}
if (allowedExtensions.Contains(ext)) //check what type of extension
{
string name = Path.GetFileNameWithoutExtension(fileName); //getting file name without extension
string myfile = name + "_" + ext; //appending the name with id
var path = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/assets/img/profiles/employeeImages"), myfile); // store the file inside ~/project folder(Img)
CurrentPicURL.SaveAs(path);
}
}
请告诉我如何在我的 EmpDetails 方法中调用此方法并将我的 CurrentPicURL 作为参数传递给此方法,此路径 "~/assets/img/profiles/employeeImages"我指的是位于我的前端项目
非常感谢我得到的所有帮助 谢谢
$('.picture').val()
将为您提供文件路径和名称 (C:\fakepath\access-matrix-design.PNG
)
如果要获取文件对象,请使用$('.picture')[0].files[0]