0

I want make display word file in pop window using mvc 4 razor. the file has been taken from specific path and the file already stored in my project.

Ganesh G
  • 1
  • 4

1 Answers1

2

If you need a popup with smaller size than the full-screen window, you could try with some javascript:

fileDownloadLink variable is your download link, pointing to the Action method in your controller The second and third arguments are explained in MDN reference for window.open function here MDN: Window.open

window.open(fileDownloadLink, 'insertPopupNameHere', 'width=400,height=400')

Otherwise you can just use an anchor tag with atribute target="_blank" (this will just open new tab in most browsers).

<a href="http://yourFileDownloadLink/" target="_blank">Preview File</a>

The code for your action method:

//Insert your mime type here, if you know it
var fileType = "application/octet-stream";

if (inline)
{
    var showInlineHeader = new System.Net.Mime.ContentDisposition
                           {
                               // for example foo.bak
                               FileName = result.FileName,

                               // always prompt the user for downloading, set to true if you want 
                               // the browser to try to show the file inline
                               Inline = inline,
                           };

    Response.AppendHeader("Content-Disposition", showInlineHeader.ToString());

    return File(result.Content, fileType);
}

I have added to my method the if statement in order to directly download files that I don't want to be previewed.

Original answer for forcing preview if availabale, taken from Returning a file to View/Download in MVC

Community
  • 1
  • 1
lyubeto
  • 125
  • 3
  • 11
  • I accept your method, its used to download the file. but i need preview the word file in popup window.... – Ganesh G Oct 29 '14 at 06:34
  • The only difference between opening file for preview and downloading it is the added header. I have added some javascript example for opening popup for preview anyway. – lyubeto Oct 29 '14 at 07:25