2012-01-03 97 views
0

我把下載鏈接在jqgrid中,我的文件存儲在服務器上不在數據庫中,文件是不同類型的(擴展名) 我想用戶應該下載文件時,他點擊下載鏈接下載文件在ASP mvc .net

代碼加載的jqGrid是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文件的代碼如下

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

}

守則控制器如下現在

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; 
    } 

不介意關於返回空值和異常處理

還表明顯示.gif注意動畫下載文件。

回答

1

我不認爲你可以使用AJAX調用來下載文件。

我覺得這個答案會讓你得到你想要的。請務必閱讀有關下載提示和MIME類型的註釋。 Download File Using Javascript/jQuery

0

我最近遇到同樣的問題,並意識到AJAX將無法下載文件。嘗試一個ActionLink的,而不是:

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

你將包括控制器的功能:

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

+0

簡單地說,AJAX不能很好地與Response.function調用工作。然而,我沒有理由說,如果有人能爲我解釋這一點,我將不勝感激。 – lohiaguitar91 2013-03-27 01:19:37

+0

這裏是我最初發布我的問題:http://stackoverflow.com/questions/15458477/response-writefile-not-working-asp-net-mvc-4-5/15577763#15577763。我使用AJAX,但沒有看到這個問題。我認爲這個bug是在控制器功能中。 – lohiaguitar91 2013-03-27 01:21:07