Google Drive Resumable upload no Response

纵饮孤独 提交于 2019-12-08 08:56:12

问题


Ok i am at a loss here as to why this doesnt work. The same code works for just uploading the metadata then the same for multip part upload. I cant get resumable upload to work.

Uploading Files Says that Step 1 should return a 200 ok with an upload_id however the following line is returning nothing. Content length is 0 there is no error response theres nothing.

string responseFromServer = reader.ReadToEnd();

My code:

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);
        }

回答1:


I finaly figured this out. The Upload id is returned in the response header.

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


来源:https://stackoverflow.com/questions/20142992/google-drive-resumable-upload-no-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!