InfinityGoesAround
InfinityGoesAround

Reputation: 1021

How to download file without specific content type

I just want to download a file. But now I can only download images.

I have this:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult DownloadFile(DownloadFileModel model, string fileName, int fileId)
        {
            //var extension = Path.GetExtension(model.FileName).TrimStart('.');
            //var extensies = Seneca.SfsLib.FileSystemHelper.UploadOptInExtensions.Contains(extension);
            string customerSchema = SfsHelpers.StateHelper.GetSchema();
            TemplateLibraryEntry entry = GetTemplateLibraryEntry(model.DesignId, customerSchema);
            FileTree tree = CreateTree(model.DesignId, entry.FilePath);           
            FileInfo fileInfo = new FileInfo(tree.Files[fileId].FullPath);
            DirectoryInfo directoryInfo = new DirectoryInfo(tree.Files[fileId].FullPath);

            try {
                var fs = System.IO.File.OpenRead(fileInfo + model.FileName );
                return File(fs, "application/jpg", fileName);
            }
            catch {
                throw new HttpException(404, "Couldn't find " + model.FileName);


            }           

        }

But If I download a file now, I see every time downloadfile. I mean I dont see the fil name and extensie

thank you

Upvotes: 4

Views: 4271

Answers (1)

Brad C
Brad C

Reputation: 2982

If you are using .NET 4.5 or newer then it has built in support to grab the MIME type straight from the filename.

MSDN link

You could just do:

return File(fs, System.Web.MimeMapping.GetMimeMapping(fileName), fileName);

Upvotes: 4

Related Questions