0

I have a Controller with an Action inside that returns an image using using FileResult.

public ActionResult GetScreeny(long id)
        {

            if (Session["userID"] == null) 
            {
                return Json("no session/cookie");
            }

            long userID = 
                Convert.ToInt64(
                    Session["userID"].ToString()
                );



            var dir = Library.AppVars.FILESERVER_IO_IMG_SAVE_PATH + userID.ToString();

            var path = Path.Combine(dir, id + ".jpg");
            return base.File(path, "image/jpeg");
        }

This is how I request the image from a View:

<img id="imgtoshow" src="@Url.Content(Library.AppVars.siteURL + "/Files/GetScreeny/" + ViewBag.screenID)" alt="IMAGE" width="100%" />

Works on Desktop, but not on Mobile :[ How does sessions/cookies works on mobile?

Oscar Ortiz
  • 813
  • 8
  • 16
  • 1
    Session is in strict bond with an `SessionId`, this was present in Cookies/Url? – adricadar Mar 20 '15 at 14:30
  • @adricadar thanks for your comment. I tried using Cookies instead of session, but the cookie was always NULL, even though I could access cookies directly. The question would be, when a browser requests an image to an URL using the "" tag, does it sends the browser cookies or is it some other kind of request? – Oscar Ortiz Mar 20 '15 at 15:52
  • 1
    In mobili phone it's possible to get `Session` **null** because `SessionId` it's not stored in `Cookies`. This is why on desktop browser *everything works fine*. – adricadar Mar 20 '15 at 15:56
  • 1
    To answer to your question, a `` don't send the image in `Cookies`, it's send the image in *someplace* to be processed (displayed). – adricadar Mar 20 '15 at 16:02
  • @adricadar thank you so much for your explanation, is there an alternative so provide images if the mobile user is logged in ? – Oscar Ortiz Mar 20 '15 at 16:38
  • You can see on mobile if user is logged in? Can you provide some small parts of code related to problem? – adricadar Mar 20 '15 at 16:42
  • @adricadar I have edited my question to add the code; if the user is not logged in, he receives a text instead of the image. Works well on Desktop, but not on mobile – Oscar Ortiz Mar 20 '15 at 16:50

1 Answers1

1

I think src might be wrong. Because you don't return a path to server you are returning an image with bytes and all the stuffs. That's why Url.Action look better.

<img id="imgtoshow" src="@Url.Action("GetScreeny", "Files", new { id = ViewBag.screenID })" alt="IMAGE" width="100%" /> 

Thare are many reasons why session work in desktop browser and in mobile browsers (your case) or other browsers don't.

The most abvious reason is that, SessionId is not set in cookies. Like the image below (this is how an SessionId looks in ASP.NET).

SessionId

You can enter Developer Tools in any browser to see the Cookies (this example is from Chrome).

Notes:

1) Session is per user, this means if you set session in browser1 and try to access it in browser2, won't work.

2) Session is stored on server side that's why needs an SessionId to get stored data for specific user (by SessionId).

Community
  • 1
  • 1
adricadar
  • 9,971
  • 5
  • 33
  • 46
  • Thank you for your good explanation. Is there an alternative so provide images if the mobile user is logged in without using cookies or sessions? – Oscar Ortiz Mar 20 '15 at 16:38
  • From a mobile: each image request is a new session apart form the page the IMG actually resides in? – Oscar Ortiz Mar 20 '15 at 16:43
  • @Rockster This is not valid for mobile or desktop. I'm guessing that mobile phone browser isn't allowed to store cookies. – adricadar Mar 20 '15 at 16:47
  • thanks again for your comment. But the session works if I check it directly from a Controller, but calling by requesting an seems to not be working, I have edited my question, please check the code – Oscar Ortiz Mar 20 '15 at 16:51