TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Rupesh N
NA
380
46.4k
error for upload image in angularjs and webmethod.
Mar 2 2020 5:03 AM
angular.service:
allsort.imgPost = (url, data) => {
return $http({
method: 'POST',
url: url,
transformRequest: angular.identity,
data: data,
headers: {
'Content-Type': undefined
}
}).then(res => res);
}
angular.controller::
$scope.btnAddProduct = () => {
$scope.product.added_date = shopFac.dateFormat(new Date());
//$scope.product.category = $scope.product.democate.name;
$scope.product.product_uni_id = shopFac.guid();
$scope.product.sub_category = "sub";
$scope.product.category = "cat";
var fd = new FormData();
angular.forEach($scope.files, (file) => {
fd.append("file", file);
});
var obj = JSON.stringify($scope.product);
fd.append("productForm", obj);
shopFac.imgPost('../admin/addproduct.aspx/createProduct', fd).then((res) => {
if (res.data.d == 0) {
console.log(res.data)
SweetAlert.swal("Success", "Successfully Add Product", "success")
$scope.product = {}
} else {
console.log(res.data);
SweetAlert.swal("Error", "Failed to add prouct.", "error")
}
})
Codebehind::
[WebMethod]
public static string createProduct(HttpContext context, string title, string description, string spec, string price, string discount_price, string category,string sub_category, string product_uni_id, string quantity, string brand, string added_date)
{
try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString))
{
context.Response.ContentType = "text/plain";
string dirFullPath = HttpContext.Current.Server.MapPath("~/img/products/");
string[] files;
string numFiles;
files = System.IO.Directory.GetFiles(dirFullPath);
//numFiles = files.Length;
numFiles = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
string str_image = " ";
foreach (string s in context.Request.Files)
{
HttpPostedFile file = context.Request.Files[s];
string fileName = file.FileName;
string fileExtension = file.ContentType;
if(!string.IsNullOrEmpty(fileName))
{
fileExtension = Path.GetExtension(fileName);
str_image = "Product_ " + numFiles.ToString() + fileExtension;
string pathSave = HttpContext.Current.Server.MapPath("~/img/products/");
file.SaveAs(pathSave);
}
}
SqlCommand cmd = new SqlCommand("insert into tbl_products values(@title, @description, @spec, @price, @discount_price, @category, @sub_category, @images, @status, @product_uni_id, @quantity, @brand, @added_date)", con);
cmd.Parameters.AddWithValue("@title", title);
cmd.Parameters.AddWithValue("@description", description);
cmd.Parameters.AddWithValue("@spec", spec);
cmd.Parameters.AddWithValue("@price", price);
cmd.Parameters.AddWithValue("@discount_price", discount_price);
cmd.Parameters.AddWithValue("@category", category);
cmd.Parameters.AddWithValue("@sub_category", sub_category);
cmd.Parameters.AddWithValue("@images", str_image);
cmd.Parameters.AddWithValue("@status", 0);
cmd.Parameters.AddWithValue("@product_uni_id", product_uni_id);
cmd.Parameters.AddWithValue("@quantity", quantity);
cmd.Parameters.AddWithValue("@brand", brand);
cmd.Parameters.AddWithValue("@added_date", added_date);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return "0";
}
}
catch(Exception ex)
{
return ex.ToString();
}
}
Error:
tell me
Reply
Answers (
1
)
401 Unauthorized in dotnet core 2.1
edit data not showing i using jquery function with mvc