2016-12-20 4 views
0

나는 어제 비슷한 질문을 올렸지 만 결과를 얻지 못했습니다. 내 검도 그리드에 데이터를로드 한 후 다운로드를 클릭하면 파일을 다운로드하려고하지만 결과를 반환하지 않습니다. 내가 다운로드하는 폴더가 프로젝트 솔루션이 아닌 서버에 있습니다. 버튼을 클릭하지 않고 다운로드를 테스트하는 컨트롤러를 만들었지 만 작동하지만 검도 버튼을 클릭하여 다운로드하고 싶습니다. 그리드아약스 호출에서 파일을 다운로드하고 강제로 결과를 다운로드 할 수 있습니다.

function showDetails(e) { 
       e.preventDefault(); 
       var dataItem = this.dataItem($(e.currentTarget).closest("tr")); 

       DownloadIndexController(dataItem.possID); 
       } 

컨트롤러에 Ajax 호출

function DownloadIndexController(possID) { 
       $.ajax({ 
        url: '@Url.Action("DownloadIndex", "Poss")', 
        type: "GET", 
        data: { possID: possID }, 
        dataType: "json", 
        success: function (data) { 
         window.location = '@Url.Action("DownloadIndex", "Poss")'; 
        } 
       }) 
      } 

컨트롤러

public ActionResult DownloadIndex(int possID) 
    { 
     string Filelocation = "myserverlocation" 
     FileContentResult ff = null; 
     try 
     { 
      OnePossModel md = new Models.OnePossModel(); 
      JsonParamBuilder myBuilder = new JsonParamBuilder(); 
      myBuilder.AddParam<Guid>("submittingUserID", System.Guid.Parse(User.Identity.GetUserId())); 
      myBuilder.AddParam<int>("possID", Convert.ToInt32(possID)); 

      string jsonReq = Models.JsonWrapper.JsonPOST(ApiBaseUrl + ApiPOSSSubBaseUrl + "/WritePOSSFile", myBuilder.GetJSonParam()); 
      string possFilename = Models.DeserialiseFromJson<string>.DeserialiseApiResponse(jsonReq); 

      possFilename = possFilename.Replace(",", ","); 

      string x = Filelocation + possFilename.ToString(); 

      var type = System.Net.Mime.MediaTypeNames.Application.Octet; 

      byte[] fileBytes = System.IO.File.ReadAllBytes(x); 
      string myfileName = possFilename; 
      ff = File(fileBytes, type,myfileName); 
      Response.AppendHeader("Content-Disposition", "attachment; filename=" + myfileName); 
      Response.BinaryWrite(fileBytes); 
      return ff; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    }    
에서 선택한 아이디를 얻기를위한 콘솔 없음 오류가

기능 컨트롤러에서

+0

를? –

+0

당신은 Response.BinaryWrite (fileBytes)를 제거해야한다는 뜻입니다; –

답변

0

, 그냥이 추가 :

public FileResult DownloadIndex(int possID) 
{ 
    . . . . 
    return File(fileBytes, type,myfileName); 
} 
+0

변경 사항을 적용했지만 아직 결과를 얻지 못했습니다. 콘솔에서도 오류가 없습니다. 나는 아무것도 반환하지 않는다 –

+0

당신의 방법을 디버그하고 당신이 bytearray을 비우지 않았는지 확인한다. – SeM

+0

나는 디버그를 가지고 있고 null을 반환하지 않는다. byte []에 데이터가 있습니다. –

0

나는 당신이하려고하는 방법을 수행 할 수 있다고 생각하지 않습니다. "ajax 파일 다운로드"를 시뮬레이트하는 해결 방법은 here을 참조하십시오.

코드에서 2 개의 요청을 만들고 있습니다. 첫 번째 파일은 파일을 만들고 응답으로 스트림 (그리고 ajax로 다운로드 할 수 없습니다) 한 다음 window.location을 설정하여 두 번째 요청을합니다. 그러나 파일은 아닙니다. 첫 번째 응답에 할당 된 이후로 "살아있다".

다음 (요구 사항에 따라 다름) 아약스 호출을 포기하고 그냥 일반 링크를 사용하는 FileResult 액션을 사용하는 경우 : 당신은 왜 * 반환 * FileResult이 파일()``에 의해 생성되지 않습니다 /poss/downloadindex/123

+0

@Isasac 나는 당신이하는 말을 정말로 이해하지 못합니다. 당신이 말하는 것을 이해할 수 있도록 코드를 수정할 수 있습니까? 감사 –