Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC FileStreamResult is not downloading file with custom file name

The pdf downloads fine but with a random name - 9619012021194536.pdf
I'm trying to set a custom name but it is not working. The downloaded file still has a random name instead of the custom name being set in code.

public ActionResult Appointment(int id)
        {
            Stream stream = null;
            string fileName = "";

            try
            {
                stream = GenerateAppointmentReport(id);
                fileName = id + "_" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".pdf";                    
            }
            catch (Exception ex)
            {
                
            }

            return new FileStreamResult(stream, MimeMapping.GetMimeMapping(fileName))
            { FileDownloadName = fileName };
        }
like image 842
Qwerty Avatar asked Dec 01 '25 06:12

Qwerty


1 Answers

You can use this code:

public ActionResult Appointment(int id)
{
  Stream stream = null;
  string fileName = "";

  try
  {
     stream = GenerateAppointmentReport(id);
     fileName = id + "_" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".pdf";                    
  }
  catch (Exception ex)
  {
                    
  }
  return new FileStreamResult(stream, "binary") { FileDownloadName = fileName };
}

I used this code and the file was downloaded with the specific name I mentioned.

like image 125
Meysam Asadi Avatar answered Dec 03 '25 20:12

Meysam Asadi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!