2012-03-01 3 views
1

동적으로 생성 된 파일을 서버에서 다운로드하려고합니다. phonegapbuild를 사용하여 간단한 응용 프로그램 시도 1.4.1.phonegap에서 다운로드 링크의 문제점 android

먼저 "content-type"및 "content-disposition : attachment; filename ="헤더가있는 http를 통해 파일을 반환하는 서버 페이지에 대한 직접 링크를 시도했습니다. 이 다운로드 링크는 일반 브라우저에서 정상적으로 작동합니다. 그러나 phonegap 응용 프로그램에서이 링크를 클릭하면 (적어도 안드로이드 2.3.3에서는) 작동하지 않는 것처럼 보입니다. 링크를 클릭하면 전화가 서버에 전달되지만 아무 반응이 없습니다.

그런 다음 FileTranfer.download라는 phonegap API 함수를 발견했습니다. 기본 다운로드 위치 (크로스 플랫폼)를 어떻게 알 수 있습니까? fileSystem.resolveFileSystemURI 함수를 시도했지만 아무 것도 성공하지 않거나 실패한 이벤트가 발생하면 가 발생하고 다음도 시도했습니다. 문장 fileSystem.root.getDirectory ("download", {create : true}); 경고가있는 다음 줄은 절대로 실행되지 않습니다.

아무도 나를 도와, 그리고 그것은 전체 경로를 찾고

+0

내 코드에 버그가있어서 fileSystem.root.getDirectory가 정상적으로 작동합니다. – Alfz

답변

0

(바람직 서버에 직접 링크를 통해) 에게 첨부 파일을 다운로드하기 위해 신뢰할 수있는 방법으로 날 지점하시기 바랍니다 수 있습니다. 그래서 "/sdcard/download.txt"와 같은 것이 작동 할 것입니다.

+0

FileTransfer.download 메서드를 의미합니까? 하지만 아마 iOS에는 "/ sdcard"경로가 없습니다. 나는 크로스 플랫폼 파일을 다운로드하는 신뢰할 수있는 방법을 찾고있다. 또한, 내 Phonegap 앱에 포함 된 WebView가 콘텐츠 처리를 감지 할 때 다운로드를 시작하지 않는 이유를 이해하지 못합니다. 첨부 파일 – Alfz

1

문제가 안드로이드 만있는 경우이 방법이 도움이 될 수 있습니다. 안드로이드에서 시도해 보았습니다. 희망 IOS에서도 작동합니다.

enter code here 
function fun(){ 
var dfd = $.Deferred(function (dfd){ 
var remoteFile = "Your link"; 
var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1); 
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) { 
fileSystem.root.getFile(localFileName, {create: true, exclusive: false}, 
    function(fileEntry) { 
var localPath = fileEntry.fullPath; 
if (device.platform === "Android" && localPath.indexOf("file://") === 0) { 
      localPath = localPath.substring(7); 
     } 

    var ft = new FileTransfer(); 
    ft.download(remoteFile, localPath, function(entry) { 
    dfd.resolve('file downloaded'); 
    // Do what you want with successful file downloaded and then 
    // call the method again to get the next file 
    //downloadFile(); 
      }, fail); 
      }, fail); 
      }, fail); 

      }); 
      return dfd.promise(); 

     } 
       fun().then(function(msg){ 
      if(msg==="file downloaded") 
      { 
      alert("Download complete"); 
      } 
      else 
      { 
      alert("Download error") 
      } 
      }); 
      function fail(){ 
      alert("error"); 
     } 
관련 문제