Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net Google Drive api将文件名上载为;无标题;_Asp.net_C# 4.0_Google Drive Api_Google Apps - Fatal编程技术网

Asp.net Google Drive api将文件名上载为;无标题;

Asp.net Google Drive api将文件名上载为;无标题;,asp.net,c#-4.0,google-drive-api,google-apps,Asp.net,C# 4.0,Google Drive Api,Google Apps,我可以上传文件到谷歌驱动器从我的网站,但我的问题是,它会显示为无标题的文件上传后 如何向上传文件添加或发布标题 谢谢 我的代码: public string UploadFile(string accessToken, byte[] file_data, string mime_type) { try { string result = ""; byte[] buffer = file_data;

我可以上传文件到谷歌驱动器从我的网站,但我的问题是,它会显示为无标题的文件上传后

如何向上传文件添加或发布标题

谢谢

我的代码:

public string UploadFile(string accessToken, byte[] file_data, string mime_type)
    {
        try
        {
            string result = "";
            byte[] buffer = file_data;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/upload/drive/v2/files?uploadType=media");

            request.Method = "POST";

            request.ContentType = mime_type;
            request.ContentLength = buffer.Length;
            request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + accessToken);

            var stream = request.GetRequestStream();
            stream.Write(file_data, 0, file_data.Length);
            stream.Close();

            HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();//Get error here
            if(webResponse.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = webResponse.GetResponseStream();
                StreamReader responseStreamReader = new StreamReader(responseStream);
                result = responseStreamReader.ReadToEnd();//parse token from result

                var jLinq = JObject.Parse(result);

                JObject jObject = JObject.Parse(jLinq.ToString());

                webResponse.Close();

                return jObject["alternateLink"].ToString();
            }

            return string.Empty;


        }
        catch
        {
            return string.Empty;
        }
    }

不使用google.api DLL就可以做到这一点并不是那么容易。在发送文件的其余部分之前,需要先发送元数据。为此,您需要使用uploadType=multipart

这应该让你开始抱歉,这是一堵代码墙。我还没有时间为此创建教程

FileInfo info = new FileInfo(pFilename);
//Createing the MetaData to send
List<string> _postData = new List<string>();
_postData.Add("{");
_postData.Add("\"title\": \"" + info.Name + "\",");
_postData.Add("\"description\": \"Uploaded with SendToGoogleDrive\",");
_postData.Add("\"parents\": [{\"id\":\"" + pFolder + "\"}],");
_postData.Add("\"mimeType\": \"" + GetMimeType(pFilename).ToString() + "\"");
_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 boundry = "foo_bar_baz";
string url = "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart" + "&access_token=" + myAutentication.accessToken;

WebRequest request = WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "multipart/related; boundary=\"" + boundry + "\"";

// Wrighting Meta Data
string headerJson = string.Format("--{0}\r\nContent-Type: {1}\r\n\r\n",
                boundry,
                "application/json; charset=UTF-8");
string headerFile = string.Format("\r\n--{0}\r\nContent-Type: {1}\r\n\r\n",
                boundry,
                GetMimeType(pFilename).ToString());

string footer = "\r\n--" + boundry + "--\r\n";

int headerLenght = headerJson.Length + headerFile.Length + footer.Length;
request.ContentLength = MetaDataByteArray.Length + FileByteArray.Length + headerLenght;
Stream dataStream = request.GetRequestStream();
dataStream.Write(Encoding.UTF8.GetBytes(headerJson), 0, Encoding.UTF8.GetByteCount(headerJson));   // write the MetaData ContentType
dataStream.Write(MetaDataByteArray, 0, MetaDataByteArray.Length);                                          // write the MetaData


 dataStream.Write(Encoding.UTF8.GetBytes(headerFile), 0, Encoding.UTF8.GetByteCount(headerFile));   // write the File ContentType
        dataStream.Write(FileByteArray, 0, FileByteArray.Length);                                  // write the file

        // Add the end of the request.  Start with a newline

        dataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
        dataStream.Close();

        try
        {
            WebResponse response = request.GetResponse();
            // 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.
            //Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
}
        catch (Exception ex)
        {
            return "Exception uploading file: uploading file." + ex.Message;

        }
FileInfo=newfileinfo(pFilename);
//正在创建要发送的元数据
列表_postData=新列表();
_添加(“{”);
_postData.Add(“\”title\”:\”+info.Name+“\”,”);
_postData.Add(“\”说明\“:\”随SendToLogleDrive一起上传\“,”);
_postData.Add(“\'parents\”:[{\'id\”:\”+pFolder+“\”}],”;
_postData.Add(“\”mimeType\”:\”+GetMimeType(pFilename.ToString()+“\”);
_postData.Add(“}”);
string postData=string.Join(“,_postData.ToArray());
byte[]MetaDataByteArray=Encoding.UTF8.GetBytes(postData);
//为文件创建数据
byte[]FileByteArray=System.IO.File.ReadAllBytes(pFilename);
字符串boundry=“foo_bar_baz”;
字符串url=”https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart“+”&access_-token=“+myAutentication.accessToken;
WebRequest=WebRequest.Create(url);
request.Method=“POST”;
request.ContentType=“多部分/相关;边界=\”“+boundary+”\”;
//写元数据
string headerJson=string.Format(“--{0}\r\n内容类型:{1}\r\n\r\n”,
边界,
“application/json;charset=UTF-8”);
string headerFile=string.Format(“\r\n--{0}\r\n内容类型:{1}\r\n\r\n”,
边界,
GetMimeType(pFilename).ToString());
字符串页脚=“\r\n--”+boundry+“--\r\n”;
int headerLenght=headerJson.Length+headerFile.Length+footer.Length;
request.ContentLength=MetaDataByteArray.Length+FileByteArray.Length+HeaderLength;
Stream dataStream=request.GetRequestStream();
dataStream.Write(Encoding.UTF8.GetBytes(headerJson),0,Encoding.UTF8.GetByteCount(headerJson));//写入元数据ContentType
dataStream.Write(MetaDataByteArray,0,MetaDataByteArray.Length);//编写元数据
dataStream.Write(Encoding.UTF8.GetBytes(headerFile),0,Encoding.UTF8.GetByteCount(headerFile));//写入文件ContentType
dataStream.Write(FileByteArray,0,FileByteArray.Length);//写文件
//添加请求的结尾。从新行开始
Write(Encoding.UTF8.GetBytes(footer),0,Encoding.UTF8.GetByteCount(footer));
dataStream.Close();
尝试
{
WebResponse=request.GetResponse();
//获取包含服务器返回的内容的流。
dataStream=response.GetResponseStream();
//使用StreamReader打开流以便于访问。
StreamReader=新的StreamReader(数据流);
//阅读内容。
字符串responseFromServer=reader.ReadToEnd();
//显示内容。
//Console.WriteLine(responseFromServer);
//清理溪流。
reader.Close();
dataStream.Close();
response.Close();
}
捕获(例外情况除外)
{
返回“异常上传文件:上传文件”+ex.消息;
}

如果你需要评论以外的解释,请告诉我。我费了一个月的劲才把它修好。这几乎和可恢复的上传一样糟糕。

不使用google.apis DLL就可以完成这项工作并不是那么容易。在发送文件的其余部分之前,需要先发送元数据。为此,您需要使用uploadType=multipart

这应该让你开始抱歉,这是一堵代码墙。我还没有时间为此创建教程

FileInfo info = new FileInfo(pFilename);
//Createing the MetaData to send
List<string> _postData = new List<string>();
_postData.Add("{");
_postData.Add("\"title\": \"" + info.Name + "\",");
_postData.Add("\"description\": \"Uploaded with SendToGoogleDrive\",");
_postData.Add("\"parents\": [{\"id\":\"" + pFolder + "\"}],");
_postData.Add("\"mimeType\": \"" + GetMimeType(pFilename).ToString() + "\"");
_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 boundry = "foo_bar_baz";
string url = "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart" + "&access_token=" + myAutentication.accessToken;

WebRequest request = WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "multipart/related; boundary=\"" + boundry + "\"";

// Wrighting Meta Data
string headerJson = string.Format("--{0}\r\nContent-Type: {1}\r\n\r\n",
                boundry,
                "application/json; charset=UTF-8");
string headerFile = string.Format("\r\n--{0}\r\nContent-Type: {1}\r\n\r\n",
                boundry,
                GetMimeType(pFilename).ToString());

string footer = "\r\n--" + boundry + "--\r\n";

int headerLenght = headerJson.Length + headerFile.Length + footer.Length;
request.ContentLength = MetaDataByteArray.Length + FileByteArray.Length + headerLenght;
Stream dataStream = request.GetRequestStream();
dataStream.Write(Encoding.UTF8.GetBytes(headerJson), 0, Encoding.UTF8.GetByteCount(headerJson));   // write the MetaData ContentType
dataStream.Write(MetaDataByteArray, 0, MetaDataByteArray.Length);                                          // write the MetaData


 dataStream.Write(Encoding.UTF8.GetBytes(headerFile), 0, Encoding.UTF8.GetByteCount(headerFile));   // write the File ContentType
        dataStream.Write(FileByteArray, 0, FileByteArray.Length);                                  // write the file

        // Add the end of the request.  Start with a newline

        dataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
        dataStream.Close();

        try
        {
            WebResponse response = request.GetResponse();
            // 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.
            //Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
}
        catch (Exception ex)
        {
            return "Exception uploading file: uploading file." + ex.Message;

        }
FileInfo=newfileinfo(pFilename);
//正在创建要发送的元数据
列表_postData=新列表();
_添加(“{”);
_postData.Add(“\”title\”:\”+info.Name+“\”,”);
_postData.Add(“\”说明\“:\”随SendToLogleDrive一起上传\“,”);
_postData.Add(“\'parents\”:[{\'id\”:\”+pFolder+“\”}],”;
_postData.Add(“\”mimeType\”:\”+GetMimeType(pFilename.ToString()+“\”);
_postData.Add(“}”);
string postData=string.Join(“,_postData.ToArray());
byte[]MetaDataByteArray=Encoding.UTF8.GetBytes(postData);
//为文件创建数据
byte[]FileByteArray=System.IO.File.ReadAllBytes(pFilename);
字符串boundry=“foo_bar_baz”;
字符串url=”https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart“+”&access_-token=“+myAutentication.accessToken;
WebRequest=WebRequest.Create(url);
request.Method=“POST”;
request.ContentType=“多部分/相关;边界=\”“+boundary+”\”;
//写元数据
string headerJson=string.Format(“--{0}\r\n内容类型:{1}\r\n\r\n”,
边界,
“application/json;charset=UTF-8”);
string headerFile=string.Format(“\r\n--{0}\r\n内容类型:{1}\r\n\r\n”,
边界,
GetMimeType(pFilename).ToString());
字符串页脚=“\r\n--”+boundry+“--\r\n”;
int headerLenght=headerJson.Length+headerFile.Length+footer.Length;
request.ContentLength=MetaDataByteArray.Length+FileByteArray.Length+HeaderLength;
Stream dataStream=request.GetRequestStream();
dataStream.Write(Encoding.UTF8.GetBytes(headerJson),0,Encoding.UTF8.GetByteCount(headerJson));//写入元数据ContentType
dataStream.Write(MetaDataByteArray,0,MetaDataByteArray.Length);//编写元数据
dataStream.Write(Encoding.UTF8.GetBytes(headerFile),0,Encoding.UTF8.GetByteCount(headerFile));//写入文件ContentType
dataStream.Write(FileByteArray