-1

I need to backup my database and I want to get that file as a download file. Simply when you get some kind of file from any website, they are just downloaded. I want to backup my database without setting a directory to that.

sqlcmd = new SqlCommand("backup database test to disk='" + backupDIR + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", con);

In this coding it sets a backup directory. I want to get that file as a downloaded file via browser.
How can this be achieved?

abarisone
  • 3,707
  • 11
  • 35
  • 54

2 Answers2

0

Try this

  public FileResult Download()
    {
        byte[] fileBytes = System.IO.File.ReadAllBytes(@"c:\folder\myfile.ext");
        string fileName = "myfile.ext";
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
    }

From here

Community
  • 1
  • 1
DanielGatti
  • 653
  • 8
  • 21
  • Actually i doesnt work well. i dont know why is that. i have created .bak file. i used your code aswell. but its nothing. – Chinthaka Hasakelum Jun 09 '16 at 12:29
  • Hi, did you replace the @"c:\folder\myfile.ext" for the current location of the file? I also recommend you using a relative path. How are you calling the method from the view? I strongly recommend you to read this, http://rachelappel.com/upload-and-download-files-using-asp-net-mvc/ . It will clarify it. – DanielGatti Jun 09 '16 at 14:25
  • Hi, Please check this code. i add this code. but its not working. i dont know why is that??i put that code as a answer – Chinthaka Hasakelum Jun 09 '16 at 17:22
0
  public ActionResult Backup()
    {
        BackupDB db = new BackupDB();

        if (db.BackupDatabase())
        {
            ViewData["Success"] = "Backup is completed Successfully";
            Download("09062016_200840.Bak");
            return View("AdminIndex");
        }

        ViewData["Exception"] = "Backup Failed";
        return View("AdminIndex");

    }
    public FileResult Download(string ImageName)
    {
        return File("‪C:\\Backups" + ImageName,System.Net.Mime.MediaTypeNames.Application.Octet);
    }
  • First of all, it doesn't have a slash. It is searching the file in C:\\Backups09062016_200840.Bak i think you are looking it in C:\\Backups\\09062016_200840.Bak Please, read this http://rachelappel.com/upload-and-download-files-using-asp-net-mvc/ it will help you a lot – DanielGatti Jun 09 '16 at 18:58