How to insert data using ASP.Net generic handler file ?RNRupesh N6yAsked Mar 8, 2020, 9:23 PM EDT2 answersShareLinkedInXCopy linkSaveHow to insert data (file, textbox) in html control. how insert both of data using generic handler file.
How to insert data using ASP.Net generic handler file ?RNRupesh N6yAsked Mar 8, 2020, 9:23 PM EDT2 answersShareLinkedInXCopy linkSaveHow to insert data (file, textbox) in html control. how insert both of data using generic handler file.
Sreekanth ReddyRank #726 · 3.2K reputation6y agoPosted Mar 9, 2020, 1:04 AM EDT0@Rupesh N $("#submit").click(function () { var file = $("#file")[0].Files[0]; var fd = new FormData(); fd.append("firstname", $("#firstname").val()); fd.append("lastname", $("#lastname").val()); fd.append("avatar", file); $.ajax({ type: "POST", contentType: false, processData: false, url: "<%= Page.ResolveUrl("~/GlobalUpdate.ashx?update=info")%>", data: fd }).success(function (data) { }); }); Do as like above.If it helps, mark as answer.Thank you. 0
RNRupesh NRank #3,763 · 393 reputation6y agoPosted Mar 9, 2020, 1:23 AM EDT0code behind ashx file :: public void ProcessRequest(HttpContext context) { //textinsert(_text); using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString)) { context.Response.ContentType = "text/plain"; int uploadCount = 0; if (context.Request.Files.Count > 0) { HttpFileCollection files = context.Request.Files; for (int i = 0; i < files.Count; i++) { HttpPostedFile file = files[i]; string fileName = new FileInfo(file.FileName).Name; // fileName = Path.Combine(context.Server.MapPath("/img/products/"), fileName); string sPath = HttpContext.Current.Server.MapPath("/img/products/"); //file.SaveAs(fileName); if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE" || HttpContext.Current.Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER" || file.ContentLength > 0) { Guid id = Guid.NewGuid(); string modifiedFileName = id.ToString() + "_" + fileName; file.SaveAs(sPath + Path.GetFileName(modifiedFileName)); string[] testfiles = file.FileName.Split(new char[] { '\\' }); sPath = testfiles[testfiles.Length - 1]; string text = context.Request["text"]; if (!File.Exists(sPath + Path.GetFileName(modifiedFileName))) { uploadCount++; if (uploadCount > 0) { SqlCommand cmd = new SqlCommand("insert into tbl_test values(@id,@filename,@title,@text)", con); cmd.Parameters.AddWithValue("@id", id); cmd.Parameters.AddWithValue("@filename", "img/products/" + modifiedFileName.ToString()); cmd.Parameters.AddWithValue("@title", fileName); cmd.Parameters.AddWithValue("@text", text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } else { context.Response.Write("File Upload"); } } } else { fileName = file.FileName; } } } context.Response.ContentType = "text/plain"; context.Response.Write("File Upload"); } } Angularjs:; $scope.uploadFile = () => { var fd = new FormData(); angular.forEach($scope.files, (file) => { fd.append("file", file); }); var obj = JSON.stringify($scope.product); fd.append("form1", obj); $http({ method: 'POST', url: "../image.ashx", data: fd, headers: { 'Content-Type': undefined } }).then(res => { console.log(res.data); }); 0Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sreekanth ReddyRank #726 · 3.2K reputationPosted Mar 9, 2020, 1:04 AM EDT
Rupesh NRank #3,763 · 393 reputationPosted Mar 9, 2020, 1:23 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts