Google ドライブ API とサンプル DrEdit プロジェクトで遊んでいます。選択した pdf ファイルをローカル ディレクトリに保存したいと思います。私の現在の手順
DownloadFile メソッドを使用してファイル コンテンツ ストリームを取得しています
public static string DownloadFile(IAuthenticator auth, String downloadUrl) { string result = ""; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(downloadUrl)); auth.ApplyAuthenticationToRequest(request); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); System.IO.Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); return reader.ReadToEnd(); } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } return result; }
次に、それを byte[] に変換して保存しようとしています。
public static string SaveFileToTmpDir(byte[] fileBytes, string fileName) { fileName = Path.Combine(Environment.GetEnvironmentVariable("temp"), fileName); using (FileStream fs = File.Create(fileName, fileBytes.Length, FileOptions.Asynchronous)) { fs.Write(fileBytes, 0, fileBytes.Length); } return fileName; }
これが私のアクションです->ビューからそのアクションへのajax呼び出しを行っています->ファイルをtmpディレクトリに保存し、保存されたファイルへのパスを返します:
[HttpPost] public JsonResult loadFile(string id) { IAuthenticator authenticator = Session["authenticator"] as IAuthenticator; DriveService service = Session["service"] as DriveService; if (authenticator == null || service == null) { Response.Redirect(Utils.GetAuthorizationUrl("", "")); } Google.Apis.Drive.v2.Data.File file = service.Files.Get(id).Fetch(); string fileStream = Utils.DownloadFile(authenticator, file.DownloadUrl); string filePath = Utils.SaveFileToTmpDir(StrToByteArray(fileStream), file.Title); return Json(new { filePath = filePath }); }
しかし、それを行った後、pfdファイルが完全に空白になります。正確なコピーを取得するためのアイデアはありますか?