2017-12-27 9 views
0

Google 드라이브 API v3 및 여전히 초보자가있는 asp.net 웹 사이트에서 작업하고 있습니다. 모든 것이 Google 드라이브 API로 잘 작동합니다. 하지만 현재 다운로드 방식에 만족하지 않습니다.Google 드라이브 API v3 .NET : 사용자가 서버가 아닌 Google 드라이브에서 직접 파일을 다운로드하도록 허용하는 방법은 무엇인가요?

protected void btnDownload_Click(object sender, EventArgs e) 
{ 

    try 
    { 
     string id = TextBox3.Text.Trim(); 
     string FilePath = google_drive.DownloadGoogleFile(id); 

     HttpContext.Current.Response.ContentType = MimeMapping.GetMimeMapping(FilePath); 
     HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(FilePath)); 
     //HttpContext.Current.Response.WriteFile(FilePath);   
     HttpContext.Current.Response.TransmitFile(FilePath); 
     HttpContext.Current.Response.Flush(); 
     HttpContext.Current.Response.End(); 
    }   
    catch (Exception ex) 
    { 
     //Handle Exception 
    } 
} 

사용자의 요청이 구글 드라이브에서 파일을 다운로드 그래서 때, 서버를 다운로드합니다 :이 뒤에 코드에서 이벤트 처리기

public static string DownloadGoogleFile(string fileId) 
    { 
      DriveService service = GetService(); //Get google drive service 
      FilesResource.GetRequest request = service.Files.Get(fileId); 

      string FileName = request.Execute().Name;     
      string FilePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Downloads"),FileName);     

      MemoryStream stream = new MemoryStream(); 

      request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) => 
      { 
       switch (progress.Status) 
       { 
        case DownloadStatus.Downloading: 
         { 
          Console.WriteLine(progress.BytesDownloaded);         
          break; 
         } 
        case DownloadStatus.Completed: 
         { 
          Console.WriteLine("Download complete."); 
          SaveStream(stream, FilePath); //Save the file 
          break; 
         } 
        case DownloadStatus.Failed: 
         { 
          Console.WriteLine("Download failed."); 
          break; 
         } 
       } 
      }; 
      request.Download(stream);     

      return FilePath; 
    } 

입니다 : 여기

샘플 코드입니다 먼저 파일을 만든 다음 다시 사용자에게 전송하십시오. 사용자가 Google 드라이브에서 직접 브라우저를 통해 파일을 다운로드하고 서버가 아닌 파일을 다운로드 할 수있는 방법이 있는지 궁금합니다. 그렇지 않은 경우 사용자가 다운로드 한 후 서버에서 다운로드 한 파일을 삭제해야한다고 생각합니다. 제발 저를 계몽하십시오. 많은 감사드립니다.

답변

0

브라우저에서 직접 파일을 다운로드하는 경우 webContentLink을 사용하는 것이 좋습니다.

브라우저에서 파일을 다운로드하는 링크. 이 드라이브에서 이진 내용이있는 파일에만 사용할 수 이다 "

분명히, 당신은 ASP를 사용하지만 드라이브 피커 사용할 때 난 그냥 JS와 함께 할 방법에 대한 조각을 공유 할 수 있습니다 :.

function downloadImage(data) { 
     if (data.action == google.picker.Action.PICKED) { 
     var fileId = data.docs[0].id; 
     //for images 
     // var webcontentlink = 'https://docs.google.com/a/google.com/uc?id='+fileId+'&export=download' 

     var webcontentlink = ' https://docs.google.com/uc?id='+fileId+'&export=download' 
     window.open(webcontentlink,'image/png'); 
     } 
    } 
+0

답을 주셔서 감사합니다. 나는 C#에서 Google 드라이브 API로 작업하는 방법을 알고 있습니다. 그리고 매우 제한된 온라인 리소스 나 샘플을 통해 v3를 배울 수 있습니다. 전체 예제 코드를 볼 수 있습니까? 클라이언트 비밀 및 액세스 토큰을 사용하여 Google 서비스를 받고 파일을 다운로드 하시겠습니까? –

관련 문제