0

I Put The Download Link in jqgrid, my Files are Stored on server not in database, files are of different types(extension) i want user should download file when he clicks on download link

Code For Loading jqgrid is as Follws

public object GetJSONFormatProjectDetails(List<ProjectMasterDTO> listProjectDTO, int SkipCount)
    {
        var data = (listProjectDTO.Select(c => new
        {
            id = c.ProjectID,
            cell = new[]
                        {
                            c.ProjectName,
                            c.OfficeName,
                            c.ProjectType,
                            c.ProjectNature,
                            c.EntrepreneurName,
                            c.Year + " Years " +c.Month  + " Months " + c.Day + " Days" ,
                            c.ConcessionWEFdate,
                            c.ProjectStartDate,
                            c.ProjectEndDate,
                            c.isRoadApplicable,
                            (c.FilePath != "NA" ) ? "<a href='#' style='color:green' onclick='DownLoadFile(\""+URLEncrypt.EncryptParameters(new string[]{ "filepath =" +c.FilePath.Replace("/","$").Replace(" ","#").Trim()})+"\");return false;'>"+(c.FilePath != "NA" ? "DownLoad":"Not Available") + " </a>" : "<span style='color:Red' >Not Available</span>"

                        }
        })).ToArray().Skip(SkipCount);
        return data;
    }

JS File Code is As Follows

function DownLoadFile(param) {
$.ajax({
url: "/Home/GetFile?parameter=" + param,
    cache: false,
    type: "POST",
    async: false
});

}

Code in Controller as follows

  public ActionResult GetFile(string parameter)
    {
        string queryStringParameters = Request.QueryString["parameter"];

        if (queryStringParameters == null)
        {
            throw new Exception("Url is tampered");
        }

        string[] parameterArray = queryStringParameters.Split('/');

        string param = null;
        string hash = null;
        string key = null;
        if (parameterArray.Length == 3)
        {
            param = parameterArray[0];
            hash = parameterArray[1];
            key = parameterArray[2];
        }
        if (!(string.IsNullOrEmpty(parameter)))
        {
            Dictionary<string, string> parameters = URLEncrypt.DecryptParameters(new string[] { param, hash, key });
            string FilePath =string.Empty ;
            parameters.TryGetValue("filepath", out FilePath);
            FilePath = FilePath.Replace('$','\\');

            // DownloadFile(FilePath);

            string name = Path.GetFileName(FilePath);
            string ext = Path.GetExtension(FilePath);
            string type = "";
            // set known types based on file extension  
            if (ext != null)
            {
                switch (ext.ToLower())
                {

                    case ".pdf":
                        type = "Application/pdf";
                        break;

                    case ".doc":

                    case ".docx":
                        type = "Application/msword";
                        break;

                    case ".jpg":

                    case ".bmp":

                    case ".tiff":

                    case ".png":

                    case ".gif":

                    case ".jpeg":
                        type = "Application/Image";
                        break;
                    default:
                        type = "Application";
                        break;

                }
            }
            Response.AppendHeader("content-disposition", "attachment; filename=" + name);

            if (type != "")
            {
                Response.ContentType = type;
            }
            String FullFilePath = @"F:\MHTOLL\ContractUploadDetails\" + name;
            //return File(new FileStream(path + fileName, FileMode.Open), "text/plain", fileName);
          //  return File(new FileStream(FullFilePath, FileMode.Open), type, name);
            return File(FullFilePath, type,name);

        }
        return null;
    }

Dont mind now about return null and exception handling

also suggest for displaying .gif animation for downloading file.

2 Answers 2

1

I don't think you can use an AJAX call to download a file.

I think this answer will get you what you want. Be sure to read the comments about the download prompt and MIME types. Download File Using Javascript/jQuery

Sign up to request clarification or add additional context in comments.

Comments

0

I recently encountered the same issue and realized that AJAX will not work to download a file. Try an ActionLink instead:

@Html.ActionLink("ButtonName", "controllerFunctionName", "controllerName", new { functionParamName = paramValue })

And you would include your function in the controller:

public ActionResult controllerFunctionName(type functionParamName){ // do your download here }

2 Comments

Simply put, AJAX doesn't work well with the Response.function calls. I don't have a reason why however, would appreciate if someone could explain this for me.
Here's where I originally posted my issue: stackoverflow.com/questions/15458477/…. I was using AJAX but did not see that as the issue. I thought the bug was in the controller function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.