使用ASP.Net是否可以让应用程序从静态位置引用文件(即./Images/logo.png),但根据哪个用户进行记录,我们会显示正确的图片?我们也希望为JSON文件执行此操作。我们想在客户端/ javascript端卸载尽可能多的逻辑。
答案 0 :(得分:1)
如果它是MVC应用,那么您可以链接到基于输入参数返回图像的MVC操作,而不是链接到/Images/logo.png。
public ActionResult ProfileImage(string id)
{
var dir = Server.MapPath("/Images");
var path = Path.Combine(dir, id + ".png"); // validate the id parameter to make sure the final path is secure
return base.File(path, "image/png");
}
然后通过控制器请求图像,例如。
<img src="@Url.Action("ProfileImage", "MyController", new { id = 12345 })" />
哪个应输出以下html:
<img src="MyController/ProfileImage?id=12345" />
对于aspx类型的项目,在ProfileImage.aspx文件中使用它:
Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", string.Format("inline; filename=\"{0}.png\"", Request("id") ));
Response.ContentType = @"image/png";
Response.WriteFile(path);
然后添加图片标记:
<img src="ProfileImage.aspx?id=12345" />
注意:强> 这是简单的方法。理想情况下,您应该删除所有&#34; id&#34;参数并使用某种用户会话从服务器端检索用户ID,而不是从客户端传递它。
答案 1 :(得分:0)
我猜不是,文件名是图像的主键。但是,如果您正在验证您的用户,那么您肯定可以通过后端传递默认图像。