Hello, i am in the process of learning ASP.net and ajax and i stumbled upon a issue. I made a web form application in which i have c# method for image upload and also a ajax function which is giving me issues.
this is my Default.aspx file:
and this is my Default.aspx.cs
When i run it i get this error in the chrome console:
I assume i didnt do the ajax call correctly, but i cant figure out the problem as im farely new in this field.
this is my Default.aspx file:
<form id="form1" runat="server"><div><asp:FileUpload ID="FileUpload1" runat="server" /><br /><br /><a href="#" id="btnImg" onclick="uploadImg()" runat="server">UPLOAD</a></div></form><script>function uploadImg(){var formData = new FormData(); formData.append('FileUpload1', $("input[type=file]")[0].files[0]); $.ajax({ type: "POST", url: 'Default.aspx/imageUpload', data: formData, contentType: 'application/json; charset=utf-8', success: function (data) { alert(data); } }); } </script>
and this is my Default.aspx.cs
protectedvoid Page_Load(object sender, EventArgs e) { } [WebMethod] protectedvoid imageUpload(object sender, EventArgs e) {if (FileUpload1.HasFile) {string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); Guid _fileNameRandom = Guid.NewGuid();string _fileNameStr = _fileNameRandom.ToString(); FileUpload1.PostedFile.SaveAs(Server.MapPath("/Images/") + (_fileNameStr + fileName)); Response.Redirect(Request.Url.AbsoluteUri); } }
When i run it i get this error in the chrome console:
Quote:Uncaught TypeError: Illegal invocation
at e (jquery.min.js:4)
at xb (jquery.min.js:4)
at Function.r.param (jquery.min.js:4)
at Function.ajax (jquery.min.js:4)
at uploadImg (Default.aspx:33)
at HTMLAnchorElement.onclick (Default.aspx:21)
I assume i didnt do the ajax call correctly, but i cant figure out the problem as im farely new in this field.