1

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

oleksii
  • 35,458
  • 16
  • 93
  • 163
HEGDE
  • 511
  • 5
  • 17
  • Are you sure the file exists? – Lloyd May 26 '16 at 09:22
  • What does _"file is downloading...contents are not downloading"_ mean exactly? – CodeCaster May 26 '16 at 09:25
  • Check that the returned file actually exists on the server. Something like this before your return statement: `if(!System.IO.File.Exists(filename)) throw new FileNotFoundException();` – oleksii May 26 '16 at 09:34
  • @Lloyd File Exists. even It is Downloading.But It Doesn't contains Contents inside. – HEGDE Aug 02 '16 at 12:37
  • @CodeCaster Files Are Downloading perfectly with zip Format,but when i open them contents are missing which are supposed to be there archived in file – HEGDE Aug 02 '16 at 12:37
  • Yeah, we can't answer that. It means that `files` does not contain what you expect it to. – CodeCaster Aug 02 '16 at 12:49
  • How can we Ask for save location each time when user clicks Download button? – HEGDE Oct 05 '16 at 07:54

2 Answers2

2

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.

Sagar R
  • 595
  • 3
  • 14
  • Actually,Content By default is **Zip**. File is downloading in Zip Format. But When I open File.It Does not Contain Anything. – HEGDE Aug 02 '16 at 12:33
1

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.

HEGDE
  • 511
  • 5
  • 17