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)