Setting Session Variable in JavaScript returns assigned variable in WebMethod

Qamar, Mo 106 Reputation points
2022-03-03T05:59:52.917+00:00

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 +'

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,246 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 25,386 Reputation points Microsoft Vendor
    2022-03-03T08:46:26.193+00:00

    Hi @Qamar, Mo ,
    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.


  2. AgaveJoe 26,186 Reputation points
    2022-03-03T13:25:45.823+00:00

    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

    0 comments No comments

  3. Qamar, Mo 106 Reputation points
    2022-03-03T14:25:00.367+00:00

    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


  4. Qamar, Mo 106 Reputation points
    2022-03-03T16:57:39.157+00:00

    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"