0

at view i have a href link as below

<a href="@Url.Content(Model.document_path)" >file</a>

and it give view as below

enter image description here

but when clicking the "file" the document is not opening.

I checked the element and its generating below html

<a href="\\.....\....\...\..\2_7.jpg">file</a>

the path is correct. but if we click "file" the document is not opening

anything iam missing??

Sachu
  • 7,555
  • 7
  • 55
  • 94
  • My bad... I didn't see the main purpose is to preview uploaded file. And how you want to display those images (using a popup after clicking image link or open in new window by demand)? – Tetsuya Yamamoto Mar 02 '17 at 09:22
  • @TetsuyaYamamoto open in new window..also it is not necessary alway .jpg..it can be pdf..word etc – Sachu Mar 02 '17 at 09:23
  • 1
    You cannot download the file from its path/filename. You need to have a method in your controller, say `public ActionResult DownLoad(int ID)` that returns a `FileResult` (and pass it the ID of the file you have saved in the db) –  Mar 02 '17 at 09:26
  • Different file types require different methods to render them in new tab or browser window - you need to return `FileResult` with proper MIME type for each file types (e.g. `application/pdf` for PDF files). – Tetsuya Yamamoto Mar 02 '17 at 09:28
  • @StephenMuecke can u give a simple example? – Sachu Mar 02 '17 at 09:29
  • It will be in GitHub project I noted in your last question, but refer [this answer](http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc) for an example of what your controller method needs to look like. –  Mar 02 '17 at 09:30
  • @StephenMuecke thanks mate – Sachu Mar 02 '17 at 09:32

2 Answers2

0

You can create an action in a controller. I use this for PDFs. You should be able to change the type using the list:

public ActionResult GetProviderPdf(string filename)
{
    string fn = "/pdf/providers/" + filename;
    return File(fn, "application/pdf", Server.UrlEncode(filename));
}

Media list for different file types:

here

0

I tried @Animus_Miles-Militis answer and it worked great! For complete reference, here's my code:

//Inside ImportController
public ActionResult GetExcelFileSample()
{
    var path = "~/App_Data/";
    var filename = "FinancialSample.xlsx";
    var type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    return File(path + filename, type, filename);
}

And in my view I had this markup:

<a href="@Url.Action("GetExcelFileSample", "Import")" download>Download</a>