Stream Html document in a new page ASP.NET MVC

1k views Asked by At

I have an object Document that describes a file: the file is HTML format file.

public class FileObj
{
    public string FileName;
    public string MimeType;
    public Stream File;
}

This is my controller Action

[HttpGet]
public ActionResult Open(Guid fileId)
{
    var document = repository.GetFile(fileId);
    return File(document.File, document.MimeType, document.FileName);
}

In the view there is a simple link like this:

<a href="/Documents/Open?fileId=1" target="blank" title="Click to download">open file</a>

Every time I click the link the file is downloaded. I want it to be opened by the browser in a new window instead.

How can I do it? I am working in ASP.NET MVC3

1

There are 1 answers

2
Chris Pratt On BEST ANSWER

You need to specify a Content-Disposition header:

Response.AppendHeader("Content-Disposition", "inline; filename=" + document.FileName);
return File(document.File, document.MimeType);

Also, I think you're looking for target="_blank". The attribute target="blank" will open in a window/tab named internally as "blank", and any further clicks will reload that window/tab instead of popping a new window/tab each time.