2012-09-06 74 views
3

我有几个页面显示图片,他们调用控制器的动作,其代码显示在[艺术post.problem是从碎纸加载的图像,不加载的时刻,但首先显示的文字,然后显示图像,我试过2负载的图像,但同样的效果更精确的方法......所以一个接一个的加载, 请你帮忙 当前代码行动渲染图像没有1-1效果

public FileContentResult ImageVew(string sourcePath) 
    { 

     string location = (string)Session["TicketFilePath"] + sourcePath; 



     byte[] myByte = System.IO.File.ReadAllBytes(location); 

     return File(myByte, "image/jpeg"); 


     Image i; 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      ms.Write(myByte, 0, myByte.Length); 
      i = Image.FromStream(ms); 
     } 
     return File(imageToByteArray(i.GetThumbnailImage(100, 100,() => false, IntPtr.Zero)), "image/png"); 
    } 

    public byte[] imageToByteArray(System.Drawing.Image imageIn) 
    { 
     MemoryStream ms = new MemoryStream(); 
     imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 
     return ms.ToArray(); 
    } 

最新版本的方法,但不是结果

public ActionResult ImageVew(string sourcePath) 
    { 
     FileInfo fi = new FileInfo((string)Session["TicketFilePath"] + sourcePath); 

     return File(fi.FullName, Utilities.MimeType(fi.Name)); 
    } 

    public ActionResult ImageVew(string sourcePath) 
    { 
     FileInfo fi = new FileInfo((string)Session["TicketFilePath"] + sourcePath); 

     byte[] imageFile = System.IO.File.ReadAllBytes(fi.FullName); 
     return new FileContentResult(imageFile, Utilities.ImageType(fi.Name)); 


    } 

    public ActionResult ImageVew(string sourcePath) 
    { 

     FileInfo fi = new FileInfo((string)Session["TicketFilePath"] + sourcePath); 
     if (fi.Exists) 
     { 
      byte[] imageFile = System.IO.File.ReadAllBytes(fi.FullName); 

      return File(imageFile, Utilities.ImageType(fi.Name)); 
     } 
     else return null; 
    } 

图片来自网络路径1个图像加载渲染1 ......但经过1加载图片兑现,和小伙子在那一刻 请帮我开发家伙

F部分的代码,然后调用动作 与渲染米cotrol

private void WriteDataRow(Class item, HtmlTextWriter writer) 
    { 
writer.RenderBeginTag(HtmlTextWriterTag.Td); 
     writer.RenderBeginTag(HtmlTextWriterTag.Center); 
     writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "20px"); 
     string src = Url.Action("ImageVew", new { sourcePath = item.Status.Marker }); 
     writer.AddAttribute(HtmlTextWriterAttribute.Src, src); 
     writer.AddAttribute(HtmlTextWriterAttribute.Alt, item.Status.StatusName); 
     writer.RenderBeginTag(HtmlTextWriterTag.Img); 
     writer.RenderEndTag(); 
     writer.RenderEndTag(); 
     writer.RenderEndTag(); 
} 

回答

2

除非您打开会话状态或使其只读取该操作,否则Asp.net mvc从不会在同一用户(同一会话)中提供多个请求。

要你需要添加

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)] 

现在,这不会与卡西尼工作(内置在Visual Studio的Web服务器),因为它只会在时间单个请求控制器或动作,但如果你测试使用IIS express或IIS,您应该看到它们使用第一种方法并行加载。

+0

这解决了可能的问题Thenks –