3

In my application I have a folder structure where you can upload all kinds of files (jpg, png, pdf, txt etc.) You can download them directly as a file or you can view the file in the browser.

To display the file in the browser, I use the folowing controller action:

    public virtual ActionResult FileDisplay(int fileId, bool isFile)
    {
        var viewmodel = _presenter.GetDocumentDisplayViewModel(fileId, isFile);
        return base.File(viewmodel.Data, viewmodel.MediaType);
    }

The file is displayed in the browser like this:

enter image description here

The browser tab shows my controller name "FileDisplay" and (for this example) the dimensions of the image.

Question: How can I display the filename in the browser tab title in stead of the controller name? This for all file types.

1. Content-Dispostion header

I have found several posts where they say to add the Content-Disposition header to the response with the filename:

But this doesn't work.

2. Add filename to the File constructor

return base.File(viewmodel.Data, viewmodel.MediaType, viewmodel.FileName);

If I do this, the file is downloaded in stead of displayed in the browser.

3. PDF file title

I have found out that sometimes the browser tab title is correct! If I display a PDF file in the browser that has a file title (title in pdf properties, not the file name) the browser title is correct: https://i.stack.imgur.com/orAZS.jpg

Can someone help me with this?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jim Craane
  • 63
  • 1
  • 6

3 Answers3

3

Workaround for now:

I have created a new action and view with a fullscreen iframe so I can set the title of this container view. The IFrame source is calling my File directly.

public virtual ActionResult FileDisplay(string fileName, int fileId, bool isFile)
{
    var viewModel = new IFrameDocumentDisplayViewModel
    {
        FileName = fileName,
        FileId = fileId,
        IsFile = isFile
    };

    return PartialView("IFrameFileDisplayView", viewModel);
}

public virtual ActionResult GetFile(int fileId, bool isFile)
{
    var viewmodel = _presenter.GetDocumentDisplayViewModel(fileId, isFile);
    return base.File(viewmodel.Data, viewmodel.MediaType);
}
@model OnView.Models.ViewModels.Document.IFrameDocumentDisplayViewModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@Model.FileName</title>
</head>
<body>
    <iframe style="border: 0; position:absolute; top:0; left:0; right:0; bottom:0; width:100%; height:100%" src="@Html.Raw(Url.Action("GetFile", "Document", new { fileId = Model.FileId, isFile = Model.IsFile}))"></iframe>
</body>
</html>

So for now I can set the page title. But I am still curious how I can accomplish it without IFrame.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jim Craane
  • 63
  • 1
  • 6
  • 2
    It is sad that this is the only method I have found to do this. A general rule of thumb, "If the solution involves an IFrame, it is probably not the right solution." – Rob S. Mar 31 '17 at 17:07
1

What the browser display as the tab title is the last part of the URL (the browser has no idea what is your controller name), so you could reformat your url like this :

localhost/OnView.Web/Document/FileDisplay/1685/True/MyTabName

Then the tab title would be MyTabName.

But changing a whole URL schema for such purpose also seems like a workaround...

Sidenote : I have the same problem, but my url is like this : FileDisplay/3?isFile=true and my tab title is 3 (in chrome. For IE, the tab title is the domain).

Kraz
  • 6,910
  • 6
  • 42
  • 70
-2

Try This:-

public virtual ActionResult FileDisplay(int fileId, bool isFile)
{
    var viewmodel = _presenter.GetDocumentDisplayViewModel(fileId, isFile);
    ViewBag.Title= viewmodel.FileName;
    return base.File(viewmodel.Data, viewmodel.MediaType);
}
Nitin
  • 290
  • 2
  • 12