2013-11-22 70 views
1

好吧,我在這裏爲什麼這不起作用的損失。相同的代碼僅用於上傳元數據,然後與多部分上傳相同。我無法獲得可恢復的上傳工作。谷歌驅動器可恢復上傳沒有迴應

Uploading Files說第1步應該返回一個200確定upload_id但是下面的行不返回任何東西。內容長度爲0,沒有錯誤響應,沒有任何內容。

string responseFromServer = reader.ReadToEnd(); 

我的代碼:

FileInfo info = new FileInfo(pFilename); 
String MimeType = GetMimeType(pFilename).ToString(); 
string footer = "\r\n"; 
//Createing the MetaData to send 
List<string> _postData = new List<string>(); 
_postData.Add("{"); 
_postData.Add("\"title\": \"" + info.Name + "\""); 
_postData.Add("}"); 
string postData = string.Join(" ", _postData.ToArray()); 
byte[] MetaDataByteArray = Encoding.UTF8.GetBytes(postData); 
// creating the Data For the file 
byte[] FileByteArray = System.IO.File.ReadAllBytes(pFilename); 

string url = "https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable"; 
int headerLenght = MetaDataByteArray.Length +footer.Length; 

WebRequest request = WebRequest.Create(url); 
request.Method = "POST"; 
request.Headers.Add("Authorization", string.Format("Bearer {0}", myAutentication.accessToken)); 
request.ContentLength = headerLenght; 
request.ContentType = "application/json; charset=UTF-8"; 
request.Headers.Add("X-Upload-Content-Type", MimeType); 
request.Headers.Add("X-Upload-Content-Length", FileByteArray.Length.ToString());   

Stream dataStream = request.GetRequestStream(); 
dataStream.Write(MetaDataByteArray, 0, MetaDataByteArray.Length); // write the MetaData 
     dataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer)); // done writeing add return just 
     dataStream.Close(); 

    try 
     { 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 


      Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
      // Display the status. IT returns OK 


      // Get the stream containing content returned by the server. 
      dataStream = response.GetResponseStream(); 

      // Open the stream using a StreamReader for easy access. 
      StreamReader reader = new StreamReader(dataStream); 

      // Read the content. 
      string responseFromServer = reader.ReadToEnd(); 

      // Display the content. ResponseFromServer returns "" nothing. 
      //Console.WriteLine(responseFromServer); 
      // Clean up the streams. 
      reader.Close(); 
      dataStream.Close(); 
      response.Close(); 
} 
catch (Exception ex) 
     { 
      return "Exception uploading file: uploading file." + ex.Message; 
      //return Error.CreateJsonError("HttpWeb.Post", "1", "Error: " + ex.Message); 
     } 
+0

我是新的谷歌雲存儲,所以你可以請告訴我如何獲得authtoken調用可恢復的上傳API。請幫幫我。 –

+0

哇這是一個非常古老的問題。但是我會使用客戶端庫,而不是http://www.daimto.com/google-drive-authentication-c/ https://github.com/LindaLawton/Google-Dotnet-Samples/tree/Genreated-samples1.0/Drive%20API https://developers.google.com/drive/v3/web/resumable-upload – DaImTo

回答

1

我finaly想通了這一點。上傳ID在響應頭中返回。

WebResponse response = request.GetResponse(); 
string Location = response.Headers["Location"];