question

QamarMo-8446 avatar image
0 Votes"
QamarMo-8446 asked LanHuang-MSFT commented

Setting Session Variable in JavaScript returns assigned variable in WebMethod

How do I get the value of a Session Variable set in JavaScript in a WebMethod?

JavaScript:
var img = new Image();
img.src = obj.imageData;
'<%Session["ImgSrc1"] = "' + img.src+'"; %>';

var canvas = document.getElementById("cnv");
var imgX = canvas.toDataURL("image/png");
<%Session["ImgSrc2"] = "'+ imgX +'";%> ;

WebMethod:
string sImage1 = HttpContext.Current.Session["ImgSrc1"].ToString(); // values[10];
string sImage2 = HttpContext.Current.Session["ImgSrc2"].ToString(); // values[10];

sImage1 returns '+ img.src+'
sImage2 returns '+ imgX +'


dotnet-aspnet-generaldotnet-aspnet-webformsdotnet-aspnet-webpages
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

How do I get the value of a Session Variable set in JavaScript in a WebMethod?

JavaScript cannot set a Session variable directly. JavaScript runs in the browser while Session exists on the server. Use AJAX to sent an HTTP request to set Session on the server.

0 Votes 0 ·

I can set a Sessions variable as the following and it works fine in WebMethod:
'<%Session["CurrentDate"] = txtFirstName.Value; %>';

But, the same won't work if I assign a variable to txtFirstName.Value and pass it to the Session.

Do you have a sample code or reference to try AJAX to send an HTTP request to set Session on the server?

0 Votes 0 ·
LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered LanHuang-MSFT edited

Hi @QamarMo-8446,
In the JavaScript function, use XmlHt tpRe quest to make an AJ AX call to the SetSe ssion WebMethod and send the values to the WebMethod.
The WebMethod then returns a string, which is displayed using a JavaSc ript al ert message box.
Here is the example.179622-345.txt
EDIT
179925-front.txt
code behind

  public static void UploadImage(string imageData)
         {          
             byte[] data = Convert.FromBase64String(imageData);
             Document pdfDoc = new Document();         
             var output = new FileStream(HttpContext.Current.Server.MapPath("2.pdf"), FileMode.Create);
             PdfWriter writer = PdfWriter.GetInstance(pdfDoc, output);
             pdfDoc.Open();
             iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(data);
              pdfDoc.Add(img);
              pdfDoc.Close();           
         }    
         protected void Button1_Click1(object sender, EventArgs e)
         {
             Process.Start(Server.MapPath("2.pdf"));
         }

179926-8.gif
Best regards,
Lan Huang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1.jpg (119.9 KiB)
1.jpg (99.7 KiB)
345.txt (1.3 KiB)
front.txt (2.1 KiB)
8.gif (142.6 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
I already have the code that you provided and that part works for my other Input (type=text) controls.
But, I have not been able to figure out how to pass an image object obj.imageData to request.send()

var img = new Image();
img.src = obj.imageData;


Basically, I have a page with several textbox controls and a Canvas control that already has a signature in it.
I can create a pdf all labels and textbox control values but I have not been able to get the signature from the Canvas
The has signature in it - This part works fine.
Now, I want to add signature to the pdf

0 Votes 0 ·

If I understand, this question has nothing to do with Session??? You want to upload an image using AJAX???

https://stackoverflow.com/questions/13198131/how-to-save-an-html5-canvas-as-an-image-on-a-server

0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered

I can set a Sessions variable as the following and it works fine in WebMethod:
'<%Session["CurrentDate"] = txtFirstName.Value; %>';

Correct it's also not possible to set Session from a text input value from Web method. ASPX Web Method are static and therefore cannot reference instance members and ASMX web services do not have a UI.

Do you have a sample code or reference to try AJAX to send an HTTP request to set Session on the server?

You did not tell us if you are working with an ASPX Web Method or ASMX Web Method. I'll assume ASPX. With either you must enable Session as Session is not turned on by default.

 [WebMethod(EnableSession = true)]
 public static string MyFunction(string jstring)
 {
     HttpContext.Current.Session["jstring"] = jstring;
     return "You send " + jstring;
 }

AJAX has the following pattern.

179731-capture.png




capture.png (21.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

QamarMo-8446 avatar image
0 Votes"
QamarMo-8446 answered LanHuang-MSFT commented

I am using aspx.
I have been able to move a bit forward and now I can save the Canvas signature in WebMethod.
string imageData = values[10];
string fileNameWitPath = @"C:\temp\" + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))

             {
                 byte[] data = Convert.FromBase64StringimageData 
                 //byte[] data = Convert.FromBase64String(imageData);
                 bw.Write(data);
                 bw.Close();
             }
         }

Now, I need to figure out how to use byte[] data to add to pdf

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Now, I need to figure out how to use byte[] data to add to pdf

You'll need to read the documentation for the PDF library you are using.

Keep in mind, you've asked three different question in this thread. Organize your thoughts and create a new posts with all the information a forum member needs to debug your code.



0 Votes 0 ·

Hi @QamarMo-8446,
I've modified my answer for your new needs, you can check it out.
If you have a new question, please open a new post.
Best regards,
Lan Huang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

0 Votes 0 ·
QamarMo-8446 avatar image
0 Votes"
QamarMo-8446 answered AgaveJoe commented

You are right but
It has been very difficult to post these questions; I get Permission Denied error. In fact, I posted a question on this error and asked why I was getting the error but no one responded.

Now I it allows me to post but ONLY sometimes and without using the option "Code Sample"

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

While I realize not being able to post JavaScript is painful it does not stop you from following the advice given and posting a screenshot of the JavaScript or a text file. Sharing incomplete and poorly formatted code forces the community to guess how your code works.

0 Votes 0 ·