2012-01-03 2 views
0

다운로드 링크를 jqgrid에 넣었습니다. 파일이 다른 유형의 데이터베이스 (확장자)가 아닌 서버에 저장되었습니다. 다운로드 링크를 클릭 할 때 파일을 다운로드해야합니다.파일을 ASP에서 다운로드 mvc .net

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

}

을 다음과 같이 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 파일의 코드와 같이로드있는 jqGrid를 들어

코드입니다

컨트롤러에 코드 NULL을 반환하고

또한 파일을 다운로드 애니메이션 .gif 참고 표시를위한 제안 예외 처리에 대해 지금

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

청춘의 마음을 다음과 같이.

답변

1

AJAX 호출을 사용하여 파일을 다운로드 할 수 있다고 생각하지 않습니다.

나는이 대답이 당신이 원하는 것을 얻을 것이라고 생각합니다. 다운로드 프롬프트 및 MIME 유형에 대한 설명을 읽으십시오. Download File Using Javascript/jQuery

0

나는 최근에 같은 문제가 발생하여 AJAX가 파일을 다운로드하지 않는다는 것을 깨달았습니다.

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

을 그리고 당신은 컨트롤러에 기능이 포함됩니다 : 대신 ActionLink을 시도

간단히 말해

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

+0

, AJAX는 Response.function 호출 잘 작동하지 않습니다. 그러나 누군가가 나를 설명 할 수 있다면 감사 할만한 이유가 없습니다. – lohiaguitar91

+0

여기에 제가 처음으로 문제를 게시 한 곳이 있습니다 : http://stackoverflow.com/questions/15458477/response-writefile-not-working-asp-net-mvc-4-5/15577763#15577763. 나는 AJAX를 사용하고 있었지만 문제로 보지 못했습니다. 버그가 컨트롤러 기능에 있다고 생각했습니다. – lohiaguitar91

관련 문제