Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to send a canvas to the server machine via Ajax and json but how do I read it?

This is the HTML
-------------------------------------------------------------------------------------------
ASP.NET
<%@ Page Language="C#" AutoEventWireup="True" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm1" %>

<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>Saving Canvas to .png file on the server</title>
	<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.2.min.js" temp_src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.2.min.js" type="text/javascript"></script>

	<script type="text/Javascript" language="Javascript">
		function drawShapes() {
			var canvas = document.getElementById("myCanvas");
			var context = canvas.getContext("2d");
			context.fillStyle = "Blue";
			context.fillRect(0, 0, 200, 200);
			context.beginPath();
			context.lineWidth = "4";
			context.strokeStyle = "Green";
			context.fillStyle = "Yellow";
			context.arc(150, 100, 50, 20, Math.PI * 2, false);
			context.stroke();
			context.fill();
		}
	</script>
</head>
<body  önload="drawShapes()">
	<canvas id="myCanvas" width="200" height="200">
	</canvas>
	<input type="button" id="btnSave" name="btnSave" value="Save the canvas to server" />
	<script type="text/javascript">
		// Send the canvas image to the server.
		$(function () {
			$("#btnSave").click(function () {
				var image = document.getElementById("myCanvas").toDataURL("image/png");
				image = image.replace('data:image/png;base64,', '');
				$.ajax({
					type: 'POST',
					url: 'WebForm1.aspx?imageDt=',
					data: '{ "imageData" : "' + image + '" }',
					contentType: 'application/json; charset=utf-8',
					dataType: 'json',
					success: function (msg) {
						alert('Image saved successfully !');
					}
				});
			});
		});

	</script>
</body>
</html>

-------------------------------------------------------------------------------------------

And here is the code behind...

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Script.Services;
using System.Web.Services;
using System.Collections.Specialized;

[ScriptService]
public partial class WebForm1 : System.Web.UI.Page
	{
	static string path = @"e:\Dev\00Test\htmlCanvas\WebApplication1\Files\";

		protected void Page_Load( object sender, EventArgs e )
		{
			if ( Page.IsPostBack )
			{

			}
			else
			{
                                //????Where do I see the stored image data????
				NameValueCollection nvc = Request.Form;

				string s = Request[ "imageData" ];
				System.Web.UI.Page p = ( System.Web.UI.Page )System.Web.HttpContext.Current.Handler;
				if ( s != null )
				{

					btnSave( s );
				}
			}
		}



		[WebMethod]
		public static void btnSave( 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();
				}
			}
		}

	}

-------------------------------------------------------------------------------------------
The url looks like.......
HTML
http://localhost:55607/WebForm1.aspx?imageDt=

and the json in it looks like (for POST)....
imageData
	"iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAANE0lEQVR4nO3dbZDVZRkG8EtajRhGd3RtNiVcyXR1DMmiNsNc0gwYQERQNNIdRKOkZjXGYRD1STJLEDQIVFBQ8CVsfIEibcuVsBA3Wgtpp..................................../MAIp6ZBxDxzDyAiGfmAUQ8Mw8g4pl5ABHPzAOIeGYeQMQz8wAinpkHEPHMPICIZ+YBRDwzDyDimXkAEc/MA4h4Zh5AxDPzACKemQcQ8cw8gIhn5gFE/Po/AQW/NxIF4akAAAAASUVORK5CYII=".

How do I tell the ASP Server to get that data that has been sent to the server?
Posted
Updated 2-Sep-13 0:44am
v5

1 solution

This is not the answer I was looking for but it does work. What I wanted is a way to see what the data is that has been send on click.

//This also associates the Servers Post Events from the Clients side to the Server.
//To setup a communication method between the Clients Machine and the server.

ASP.NET
<%@ Page Language="C#" AutoEventWireup="True" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm1" %>

<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>Saving Canvas to .png file on the server</title>
	<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.2.min.js" temp_src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.2.min.js" type="text/javascript"></script>

	<script type="text/Javascript" language="Javascript">
		function drawShapes() {
			var canvas = document.getElementById("myCanvas");
			var context = canvas.getContext("2d");
			context.fillStyle = "Blue";
			context.fillRect(0, 0, 200, 200);
			context.beginPath();
			context.lineWidth = "4";
			context.strokeStyle = "Green";
			context.fillStyle = "Yellow";
			context.arc(150, 100, 50, 20, Math.PI * 2, false);
			context.stroke();
			context.fill();
		}
	</script>
</head>
<body  önload="drawShapes()">
	<canvas id="myCanvas" width="200" height="200">
	</canvas>
	<input type="button" id="btnSave" name="btnSave" value="Save the canvas to server" />
	<script type="text/javascript">
		// Send the canvas image to the server.
		$(function () {
			$("#btnSave").click(function () {
				var image = document.getElementById("myCanvas").toDataURL("image/png");
				image = image.replace('data:image/png;base64,', '');
				$.ajax({
					type: 'POST',
					url: 'WebForm1.aspx/UploadImage',  //Must Point to a aspx type page and with the / followed by the name of the method.
					data: '{ "imageData" : "' + image + '" }',
					contentType: 'application/json; charset=utf-8',
					dataType: 'json',
					success: function (msg) {
						alert('Image saved successfully !');
					}
				});
			});
		});

	</script>
</body>
</html>

C#
//Here is the Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Script.Services;
using System.Web.Services;
using System.Collections.Specialized;

[ScriptService]
public partial class WebForm1 : System.Web.UI.Page
{
	static string path = @"e:\Dev\00Test\htmlCanvas\WebApplication1\Files\";
	protected void Page_Load( object sender, EventArgs e ){}

	[WebMethod] //This ensures that the server calls this method on post.
	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();
			}
		}
	}
}
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900