2012-02-13 6 views

risposta

20

ritorno FilePathResult utilizza File metodo di controllo

public ActionResult GetMyImage(string ImageID) 
{ 
    // Construct absolute image path 
    var imagePath = "whatever"; 

    return base.File(imagePath, "image/jpg"); 
} 

ci sono diversi overloads of File metodo. Usa ciò che è più appropriato per la tua situazione. Ad esempio, se si desidera inviare l'intestazione Content-Disposition in modo che l'utente ottenga la finestra di dialogo SaveAs anziché visualizzare l'immagine nel browser, si passa al terzo parametro string fileDownloadName.

+3

Wow, devi amare ASP.NET MVC. –

1

Semplicemente provare uno di questi a seconda della situazione (copiato da here):

public ActionResult Image(string id) 
{ 
    var dir = Server.MapPath("/Images"); 
    var path = Path.Combine(dir, id + ".jpg"); 
    return base.File(path, "image/jpeg"); 
} 


[HttpGet] 
public FileResult Show(int customerId, string imageName) 
{ 
    var path = string.Concat(ConfigData.ImagesDirectory, customerId, @"\", imageName); 
    return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg"); 
} 
4

È possibile utilizzare FileContentResult come questo:

byte[] imageData = GetImage(...); // or whatever 
return File(imageData, "image/jpeg"); 
2
using System.Drawing; 
using System.Drawing.Imaging;  
using System.IO; 

public ActionResult Thumbnail() 
{ 
    string imageFile = System.Web.HttpContext.Current.Server.MapPath("~/Content/tempimg/sti1.jpg"); 
    var srcImage = Image.FromFile(imageFile); 
    var stream = new MemoryStream(); 
    srcImage.Save(stream , ImageFormat.Png); 
    return File(stream.ToArray(), "image/png"); 
} 
Problemi correlati