Google云端硬盘可恢复上传无响应 [英] Google Drive Resumable upload no Response

查看:180
本文介绍了Google云端硬盘可恢复上传无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我为此感到茫然,为什么这不起作用。相同的代码仅用于上传元数据,然后与多部分上传相同。我无法获得可恢复的上传工作。

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.

上传文件说第1步应该返回一个200确定与upload_id然而下面的行没有返回。内容长度为0,没有错误响应,没有任何内容。

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


推荐答案

我最终明白了这一点。上传ID在响应头中返回。

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

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

这篇关于Google云端硬盘可恢复上传无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆