简体   繁体   中英

Download ZIP file From path stored in Database MVC C#

Need to download zip file content, path is stored in sqlserver, i am using FILERESULT Action method in controller. My Controller looks like this:

public FileResult Download(string id)
{
    string txtvalue = null; int cat = 0;

    string searchType = Session["type"].ToString();
    if (searchType == "name")
    {
        txtvalue = Session["item"].ToString();
    }
    else if (searchType == "cat")
    {
        cat = Convert.ToInt32(Session["item"]);
    }
    int fid =Convert.ToInt32(Session["Fid"]);
    var files = _urepo.GetprojectName(fid);
    string filename = (from f in files
                       select f.PRJ_LOCATION).SingleOrDefault();
    string contentType = "application/zip";

    return File(filename, contentType,"download"+filename);
}

But only file is downloading...contents are not downloading.. any help will be apprciated

Try something like this,first of all you need to check whether you are fetching file from Database or not? so condition will help you,another thing try to change "Content Type"

if (filename != null)
 {
      contentType = "application/force-download";
      return File(filename, contentType, Path.GetFileName(filename));
 }

Note : I have not posted full code,but the only place where chances mistake.

I Got It , Actually As CodeCaster Said, Contents were Missing While Archiving . Later I appended data With Ajax Call.

public FileResult Download()
        {
            int Id = Convert.ToInt32(TempData["FileID"]);

            var files = _urepo.GetprojectName(Id);
            string filename = (from f in files
                               select f.PRJ_LOCATION).SingleOrDefault();
            string contentType = "application/zip";
           byte[] fileBytes = System.IO.File.ReadAllBytes(filename);

            string file=filename.Substring(filename.LastIndexOf("\\")+1);
             return File(fileBytes, contentType, file);
        }

Thank You All.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM