在移动设备上使用 FileResult return 图像时会话数据为空

Session data is null while Using FileResult to return an image using mobile

我有一个带有 Action 的控制器,returns 使用 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");
        }

这是我从视图中请求图像的方式:

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

适用于桌面设备,但不适用于移动设备:[ sessions/cookies 如何在移动设备上工作?

我认为src可能是错误的。因为你没有 return 到服务器的路径 你正在 returning 图像 字节和所有东西。那是 why Url.Action 看起来更好。

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

有很多原因 why session work 在桌面浏览器中 在移动浏览器中 (你的情况)或其他浏览器 不要t.

最明显的原因是,SessionId没有在cookies中设置。如下图所示(这是 SessionId 在 ASP.NET 中的样子)。

你可以在任何浏览器中输入Developer Tools来查看Cookies(本例来自Chrome).

备注:

1) 会话 每个用户,这意味着如果您 browser1 中设置会话并尝试在 browser2 中访问它,将无法正常工作。

2) 会话存储在服务器端 ,这就是为什么需要 SessionId 来获取特定用户的存储数据(通过 SessionId)。