0

I have an MVC5 project, and it has a view that allows a user to download documents from SQL Server. This view is a form page with one or more file input controls that allow a user to upload any file types. When a user selects a file from the desktop and puts it into the input file box and clicks on the submit button, the file is converted into a byte[] type and saved into SQL Server. After the file is uploaded, the user opens the same page again, and the download link appears in the control. If a user clicks on it, the file isn’t saved onto their desktop, but if the user right-clicks on the link and opens it in a new tab, the file can be downloaded. Here is the following snippet from the view page that allows user to download files from upload control:

FillTemplateFields.cshtml

@{
     string[] fileUploadItems = @value.Split(',');
 }

@for (int k = 0; k < @fileUploadItems.Length; k++)
{
     <div id="fileContainer-@count">
     @if(fileUploadItems[k] != null){
         <input id="sub-file-@count" type="file" value="@fileUploadItems[k]" placeholder="@control.Placeholder" name="fileUpload-@count" data-sub-channel="sub-input-@count">

         @Ajax.ActionLink("Download " + @fileUploadItems[k], "DownloadFile", new { fieldID = @count, fileName = @fileUploadItems[k] }, null)
         <br />
 }
 else
 {
     <input id="sub-file-@count" type="file" value="" placeholder="@control.Placeholder" name="fileUpload-@count" data-sub-channel="sub-input-@count">
 }

 <a id="deleteControl-@count" href="#">Delete</a>

The ‘fileUploadItems’ object contains the file names saved into the upload control. The Ajax.ActionLink helper creates a link that calls for the ‘DowloadFile’ method, which passes the the values of the fielded and the name of the file, and finds the file that was saved onto Sql Server:

TemplateValuesController.cs

public FileResult DownloadFile(int fieldID, string fileName)
    {
        tbl_FileStorage file = db.tbl_FileStorage.Where(f => f.FileName == fileName && f.TemplateFieldValuesID == fieldID).First();
        return File(file.File, System.Net.Mime.MediaTypeNames.Application.Octet, file.FileName);
    }

I can download the file successfully by right-clicking on the download link and opening it on new tab, but I can’t download it while staying on the same page, and I’m not trying to stream the file on a new browser tab. Can someone give me solutions to what I want? Thank you.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
J. Zacka
  • 139
  • 2
  • 2
  • 12
  • Try adding a target="_blank" attribute to your actionlink – Bashkim Meka Sep 20 '16 at 15:25
  • Your question already seems to be answered here: http://stackoverflow.com/questions/730699/how-can-i-present-a-file-for-download-from-an-mvc-controller – easuter Sep 20 '16 at 15:29
  • @BashkimMeka I tried what you suggested. I added the attribute, target="_blank", to the actionlink helper, clicked on the link, and it didn't download the files I wanted to download. – J. Zacka Sep 20 '16 at 19:26
  • @easuter I followed the link you suggested, tried the methods found, and I still couldn't get any uploaded files to be saved onto my desktop. – J. Zacka Sep 20 '16 at 19:27

1 Answers1

0

The problem has been resolved. I fixed it by changing one line of code in the “FillTemplateFields.cshtml” page.

Before Change

@Ajax.ActionLink("Download " + @fileUploadItems[k], "DownloadFile", new { fieldID = @count, fileName = @fileUploadItems[k] }, null)

After Change

@Html.ActionLink("Download " + @fileUploadItems[k], "DownloadFile", new { fieldID = @count, fileName = @fileUploadItems[k] })

After I made the change, I can download the file onto my desktop while staying on the page where the download link is found and not having to open the link in a new tab.

J. Zacka
  • 139
  • 2
  • 2
  • 12