Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • TheOriginal 22 posts 122 karma points
    Feb 11, 2016 @ 09:20
    TheOriginal
    0

    Ajax upload on front end of Umbraco project

    I am trying to upload an image from the front end and change a current image to the uploaded image, but i keep getting a 500 (Internal Server Error) error. Here's my code:

    My Controller:

    public ActionResult FileUpload(HttpPostedFileBase file)
        {
            if(file != null)
            {
                string image = System.IO.Path.GetFileName(file.FileName);
                string path = System.IO.Path.Combine(Server.MapPath("~/Images/Uploads/"), image);
                if(IsImage(file))
                {
                    file.SaveAs(path);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        file.InputStream.CopyTo(ms);
                        byte[] array = ms.GetBuffer();
                    }
                }
            }
            return View(file);
        }
        private Boolean IsImage(HttpPostedFileBase file)
        {
            Boolean isImage = false;
            string image = System.IO.Path.GetFileName(file.FileName);
            string path = System.IO.Path.Combine(Server.MapPath("/~Images/Uploads/"), image);
    
            if (file != null)
            {
                if(file.ContentLength > 500)
                {
                    string extension = Path.GetExtension(path);
                    if(extension == ".png" || extension == ".jpg" || extension == ".tif" || extension == ".gif")
                    {
                        isImage = true;
                    }
                }
            }
            return isImage;
        }
    

    My Ajax:

    $.ajax({
            url: "/Umbraco/Surface/Upload/FileUpload/",
            type: 'POST',
            dataType: 'json',
            processData: false,
            data: data,
            success: function(data) {
                console.log("Success!");
                console.log(data);
            },
            error: function(data) {
                console.log("Error!");
                console.log(data);
            }
        })
    

    If anyone can help It'd be a huge help!

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Feb 11, 2016 @ 10:24
    Nik
    0

    Well the first question is, is it getting to your upload action? Have you stepped through to find out on what line the issue is occurring and have you managed to identify the stack trace of the 500 error to identify the real error?

Please Sign in or register to post replies

Write your reply to:

Draft