Download file of any type in Asp.Net MVC using FileResult?

后端 未结 9 1691
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 05:05

I\'ve had it suggested to me that I should use FileResult to allow users to download files from my Asp.Net MVC application. But the only examples of this I can find always h

相关标签:
9条回答
  • 2020-11-22 05:21

    The MVC framework supports this natively. The System.Web.MVC.Controller.File controller provides methods to return a file by name/stream/array.

    For example using a virtual path to the file you could do the following.

    return File(virtualFilePath, System.Net.Mime.MediaTypeNames.Application.Octet,  Path.GetFileName(virtualFilePath));
    
    0 讨论(0)
  • 2020-11-22 05:26

    GetFile should be closing the file (or opening it within a using). Then you can delete the file after conversion to bytes-- the download will be done on that byte buffer.

        byte[] GetFile(string s)
        {
            byte[] data;
            using (System.IO.FileStream fs = System.IO.File.OpenRead(s))
            {
                data = new byte[fs.Length];
                int br = fs.Read(data, 0, data.Length);
                if (br != fs.Length)
                    throw new System.IO.IOException(s);
            }
            return data;
        }
    

    So in your download method...

            byte[] fileBytes = GetFile(file);
            // delete the file after conversion to bytes
            System.IO.File.Delete(file);
            // have the file download dialog only display the base name of the file            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, Path.GetFileName(file));
    
    0 讨论(0)
  • 2020-11-22 05:29

    Thanks to Ian Henry!

    In case if you need to get file from MS SQL Server here is the solution.

    public FileResult DownloadDocument(string id)
            {
                if (!string.IsNullOrEmpty(id))
                {
                    try
                    {
                        var fileId = Guid.Parse(id);
    
                        var myFile = AppModel.MyFiles.SingleOrDefault(x => x.Id == fileId);
    
                        if (myFile != null)
                        {
                            byte[] fileBytes = myFile.FileData;
                            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, myFile.FileName);
                        }
                    }
                    catch
                    {
                    }
                }
    
                return null;
            }
    

    Where AppModel is EntityFramework model and MyFiles presents table in your database. FileData is varbinary(MAX) in MyFiles table.

    0 讨论(0)
提交回复
热议问题