Skip to content

How to upload html5 canvas image in server

Dinesh Ambaliya

Asked Oct 19, 2012, 2:10 AM EDT1 answer
Share
LinkedInX
I have written this code as illustrated in
saving-html-5-canvas-as-image-on-the-server-using-aspnet

       

       

 

       

 

   

Default.aspx.cs

PowerShell
using System;
using System.IO;
using System.Web.Script.Services;
using System.Web.Services;

[ScriptService]
public partial class _Default : System.Web.UI.Page
{
    static string path = @"F:\Dinesh\";

 

    [WebMethod()]

C#
    public static void UploadImage(string imageData)
    {
        string fileNameWitPath = path + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
        using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
        {
            using (BinaryWriter bw = new BinaryWriter(fs))
            {
                byte[] data = Convert.FromBase64String(imageData);
                bw.Write(data);
                bw.Close();

            }
        }
    }
}

Now, drawShapes() is working and I can see the graphics in canvas when view in browser. But when I click the button to save that canvas image in folder nothing is happen. How can I save that canvas image in my folder?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • +1
    I have solved it. Problem was

    $.ajax({

                            type: 'POST',

                            url: 'CanvasSave.aspx/UploadImage', //correction:- url: 'Default.aspx/UploadImage'.

                            data: '{ "imageData" : "' + image + '" }',

                            contentType: 'application/json; charset=utf-8',

                            dataType: 'json',

    Code
                            success: function(msg) {
                                alert('Image saved successfully !');
                            }
                        });
    +1