1

Could someone please help me out, I just can't figure out what's wrong.

The scenario: Employees can upload files (doc, docx or pdf). These files are displayed in a table. When they click on the name, the file should open in a new window.

Right now I can upload a file. This file is saved in the uploads folder and in the database. The uploaded files are displayed in a table, but when I click the name of the file, the file gets downloaded in stead of opened in a new window. I've tried several things, but I always get the same result.

I'll include the controller and the view next.

Controller:

//To view the files in a table.
public ActionResult ShowUploadedFiles()
{
    return View(db.UploadedFiles.ToList());
}
//To open a file
public ActionResult OpenFile(string fileName)
{
    var fs = System.IO.File.OpenRead(Server.MapPath("~/Uploads/" + fileName));
    return File(fs, "application/pdf", fileName);
}

View

@foreach (var file in Model) 
{
    <tr>
        <td>@Html.DisplayFor(f => file.FileDate)</td>
        <td>@Html.DisplayFor(f => file.Employee.FullName)</td>
        <td><a href="/Uploads/@file.FileName" target="_blank">@file.FileName</a></td>
        <td>@Html.DisplayFor(f => file.FileName)</td>
        <td>@(file.FileSize/1000)kb</td>
        <td><a href="@Url.Action("OpenFile", new { fileName=file.FileName})" target=_blank>@file.FileName</a></td>            
    </tr>
}

The first href "works". When I click it the pdf opened in a new window but the docx file got downloaded. When I entered the url myself in a new window I got the same result (pdf opened, docx downloaded). When I clicked the second href both files got downloaded.

Is there something wrong or is it because I do this in Chrome? (I just checked it in explorer 9 and I got the same results)

3 Answers3

0

it's expected behaviour of the browser.

  • In your first href you are opening in _blank window. all browser are natively supports pdf content type, for that reason pdf opened in the window itself.
  • for second href your writing to response output. basically you are telling the browser to download the things for me.

if you want show the file in window itself, then add the content disposition headers in your response. for more info see this answer Returning_file_in_Asp.net mvc3

var cd = new System.Net.Mime.ContentDisposition
    {
        FileName = yourfilename, 

        // always prompt the user for downloading, set to true if you want 
        // the browser to try to show the file inline
        Inline = true, 
    };
  Response.AppendHeader("Content-Disposition", cd.ToString());
Community
  • 1
  • 1
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
0

I think the pdf file opens because most new browsers has a blugin 'ex. adobe reader' to read or render PDF files but docx files can't open directly like pdf ,, you have to find a tool to render your docx file.

0

The reason why PDFS open in the browser in your first href, is because most browsers have a built-in PDF viewer, while for office documents, you would have to install external plugins to allow viewing in the browser.

See: http://bit.ly/ZiwpCw (for Chrome)

In the second href, you're linking to an action which returns the file directly, so the browser sees it as a download and not that you're trying to view it in another window.

Rares Matei
  • 286
  • 1
  • 12