Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

ASP.net ajax image upload

$
0
0
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:

<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.

Viewing all articles
Browse latest Browse all 3938

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>