0

I am implementing a system where the user can upload any number of documents of almost any file type to be linked to a "work item". Essentially, the user can generate a task, known as a work item, and attach any number of files to it. I've implemented the components to handle the work item itself, uploading the files, managing the files (editing and deleting), but I am running into problems viewing the files.

When the user uploads a file, the file is saved in a network folder. All users have read-access to all files in the directory. The path to the file is saved in the database, which I access through Entity Framework.

My current issue is that I can't find a generic way to open those files. I have implemented a controller to stream the file to the browser, but this only works for certain file types.

Specifically, I've tried:

  • Creating a hyperlink and putting the file path in the href tag, but this tries to open the file path relative to the application's location on the webserver. Basically, it appends the file path to the current URL.

    • Example:

      • href="@Path.Combine(item.PATH, item.NAME)"
    • Output:

      • href="\\server\myapp\item\3\test.jpg"
      • But when I click on this anchor, it is opened relative to my current URL.
  • Creating a controller to stream the file via a WebClient, but this only works for specific file types.

In summary, is there a way to open any file from MVC, either through the browser or by opening it on the client computer?

ArunPratap
  • 4,020
  • 7
  • 22
  • 39
Jack
  • 1,283
  • 12
  • 29

1 Answers1

0

After asking the question, I remembered that I could just download the file to the client. At that point, they can open it if they wish.

I added an Action to my Controller to download it from the network location:

// GET: Viewer/File
[HttpGet]
public FileResult Download(string path)
{
   byte[] fileBytes = System.IO.File.ReadAllBytes(path);
   string fileName = Path.GetFileName(path);
   return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

Taken from this question.

Jack
  • 1,283
  • 12
  • 29