Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 01:01

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



ASP.net c# ajax image upload

Discussion on ASP.net c# ajax image upload within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Apr 2012
Posts: 370
Received Thanks: 20
ASP.net c# ajax image upload

I have a issue with a image uploader i am making in ASP. I want to upload a image to the projects folder using ajax.

my html and js:

HTML Code:
   <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', $('#btnImg')[0].files[0]);
        $.ajax({
            type: "POST",
            url: 'Default.aspx/imageUpload',
            data: formData,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                alert(data);
            }
        });

    }
</script>
my image upload code(c#):

Code:
[WebMethod]
protected void 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);
    }
}
IN console i get the following errors: Uncaught TypeError: Cannot read property '0' of undefined at uploadImg (Default.aspx:32) at HTMLAnchorElement.onclick (Default.aspx:21)

line 32 in default.aspx is: }); of the js script. and line 21 is: function uploadImg(){

Hope anyone can help me resolve this.
Hikarim is offline  
Old 03/10/2017, 17:58   #2
 
ecks de's Avatar
 
elite*gold: 0
The Black Market: 180/0/1
Join Date: Apr 2016
Posts: 1,452
Received Thanks: 201
well. first of all u shouldn't upload the data directly into your project folder. this is a huge security gap and can get attacked quite easily. So you should add file validation. But however try this:
Code:
<asp:FileUpload ID="uploadIMG" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload Image" OnClick="Upload" />

protected void Upload(object sender, EventArgs e)
{
    if (uploadIMG.HasFile)
    {
        string fileName = Path.GetFileName(uploadIMG.PostedFile.FileName);
        uploadIMG.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
    }
}
ecks de is offline  
Reply


Similar Threads Similar Threads
Lawl Image - Easy Image upload
10/08/2015 - Coding Releases - 132 Replies
Hier steht euch nun die gründlich überarbeitete Version von LawlImage zur verfügung! Mit LawlImage ist es einfach wie nie, schnell Bilder hochzuladen. Das einzige was ihr tun müsst, ist einen Rechtsklick auf das Bild auszuführen und unter "Senden an" oder "Send To" LawlImage zu wählen. Schneller kann man Bilder nicht hochladen! http://image.lawl-dev.com/imgsrv.php?url=79348bed 8d5da002ffc10d36f2e916d2.png
Upload picture with jquery or ajax with multiple fields
02/01/2015 - Web Development - 3 Replies
Hi I need to know how i can make form to upload picture without submiting form and get picture path in form field to insert in database here is code so i need button next to image field "SELECT" and when i press select to choose picture from computer, image get uploaded and i get path im image field like C:\xampp\htdocs\images\users\imageblabla.jpg so i can save it into dataabse. form html : <form action="" method="POST">
Image Upload Hilfe!
01/11/2013 - Web Development - 1 Replies
Hallo epvp ich hab eben eine seite erstelle wo man bilder uploaden kann und wenn ich ein bild uploade steht da bad request : Chevereto image hosting script Hab es richtig configuriert kann mir jemand helfen?



All times are GMT +2. The time now is 01:01.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.