How to download MP3 files with ASP.NET MVC 5 & Entity Framework

asp.net asp.net-mvc c# entity-framework entity-framework-6

Question

I have successfully done the upload bit - uploaded the MP3 files to my SQL Server, but I need help in downloading the files.... I use the following code to upload the MP3 file

public ActionResult Create([Bind(Include = "ID,Artist,Album")] TrackUpload trackUpload, HttpPostedFileBase upload)
{
    try
    {
        if (ModelState.IsValid)
        {
            if (upload != null && upload.ContentLength > 0)
            {
                var song = new File
                {
                    FileName = System.IO.Path.GetFileName(upload.FileName),
                    FileType = FileType.Songs,
                    ContentType = upload.ContentType
                };
                using (var reader = new System.IO.BinaryReader(upload.InputStream))
                {
                    song.Content = reader.ReadBytes(upload.ContentLength);
                }
                trackUpload.Files = new List<File> { song };
            }
            db.TrackUploads.Add(trackUpload);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }

    catch (RetryLimitExceededException /* dex */)
    {

        ModelState.AddModelError("", "Cant save changes.");
    }
    return View(trackUpload);
}

It is working fine which meaning the file stores in Database, Now how do i download the MP3 file from sql db using entity framework

1
0
7/11/2016 10:35:52 PM

Accepted Answer

To download an MP3 file from MVC controller, make sure you have set MIME type of FileContentResult either as audio/mp3, audio/mpeg3 or audio/x-mpeg3 (see http://filext.com/file-extension/MP3 for details).

Here is a simple controller code defining file downloading scenario:

/*
 *  assume Id becomes file ID key to download
 *  this code provides simple download function by returning FileContentResult with proper MIME type
 */
public FileContentResult DownloadFile(int Id) 
{
    // assume FileContext is your file list DB context with FileName property (file name + extension, e.g. Sample.mp3)
    using (FileContext db = new FileContext())
    {
        var download = db.TrackUploads.Where(x => x.ID == Id).SingleOrDefault();
        if (download != null) 
        {
            // remove this line if you want file download on the same page
            Response.AddHeader("content-disposition", "inline; filename=" + download.FileName);
            return File(download.FileName, "audio/mp3");
        }
        else
        {
            return null;
        }
    }
}

Addendum

To get MIME type from file extension before downloading, this code part may work:

if (download != null) 
{
    String mimeType = MimeMapping.GetMimeMapping(download.FileName);
    ...
    // other tasks
    ...
    return File(download.FileName, mimeType);
}

Reference: view and download File from sql db using Entity FrameWork (with adjustment for MP3 extension)

1
5/23/2017 12:08:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow