本文整理汇总了C#中File.ToJSON方法的典型用法代码示例。如果您正苦于以下问题:C# File.ToJSON方法的具体用法?C# File.ToJSON怎么用?C# File.ToJSON使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类File
的用法示例。
在下文中一共展示了File.ToJSON方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UploadFile
/// <summary>
/// Upload a file.
/// </summary>
/// <param name="file">File metadata.</param>
/// <param name="data">Data.</param>
/// <returns>AsyncSuccess with File or Exception for error.</returns>
/// <example>
/// Upload a file to the root folder.
/// <code>
/// var bytes = Encoding.UTF8.GetBytes("world!");
///
/// var file = new GoogleDrive.File(new Dictionary<string, object>
/// {
/// { "title", "hello.txt" },
/// { "mimeType", "text/plain" },
/// });
///
/// StartCoroutine(drive.UploadFile(file, bytes));
/// </code>
/// Update the file content.
/// <code>
/// var listFiles = drive.ListFilesByQueary("title = 'a.txt'");
/// yield return StartCoroutine(listFiles);
///
/// var files = GoogleDrive.GetResult<List<GoogleDrive.File>>(listFiles);
/// if (files != null && files.Count > 0)
/// {
/// var bytes = Encoding.UTF8.GetBytes("new content.");
/// StartCoroutine(drive.UploadFile(files[0], bytes));
/// }
/// </code>
/// </example>
public IEnumerator UploadFile(File file, byte[] data)
{
#region Check the access token is expired
var check = CheckExpiration();
while (check.MoveNext())
yield return null;
if (check.Current is Exception)
{
yield return check.Current;
yield break;
}
#endregion
string uploadUrl = null;
// Start a resumable session.
if (file.ID == null || file.ID == string.Empty)
{
var request = new UnityWebRequest(
"https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable");
request.method = "POST";
request.headers["Authorization"] = "Bearer " + AccessToken;
request.headers["Content-Type"] = "application/json";
request.headers["X-Upload-Content-Type"] = file.MimeType;
request.headers["X-Upload-Content-Length"] = data.Length;
string metadata = JsonWriter.Serialize(file.ToJSON());
request.body = Encoding.UTF8.GetBytes(metadata);
var response = new UnityWebResponse(request);
while (!response.isDone)
yield return null;
if (response.statusCode != 200)
{
JsonReader reader = new JsonReader(response.text);
var json = reader.Deserialize<Dictionary<string, object>>();
if (json == null)
{
yield return new Exception(-1, "UploadFile response parsing failed.");
yield break;
}
else if (json.ContainsKey("error"))
{
yield return GetError(json);
yield break;
}
}
// Save the resumable session URI.
uploadUrl = response.headers["Location"] as string;
}
else
{
uploadUrl = "https://www.googleapis.com/upload/drive/v2/files/" + file.ID;
}
// Upload the file.
{
var request = new UnityWebRequest(uploadUrl);
request.method = "PUT";
request.headers["Authorization"] = "Bearer " + AccessToken;
request.headers["Content-Type"] = "application/octet-stream"; // file.MimeType;
request.body = data;
var response = new UnityWebResponse(request);
//.........这里部分代码省略.........
示例2: DuplicateFile
/// <summary>
/// Duplicate a file in a specified folder.
/// </summary>
/// <param name="file">File to duplicate.</param>
/// <param name="newTitle">New filename.</param>
/// <param name="newParentFolder">
/// New parent folder. If it is null then the new file will place in root folder.
/// </param>
/// <returns>AsyncSuccess with File or Exception for error.</returns>
/// <example>
/// Copy a file to the root folder.
/// <code>
/// StartCoroutine(drive.DuplicateFile(someFile, someFile.Title, null));
/// </code>
/// </example>
public IEnumerator DuplicateFile(File file, string newTitle, File newParentFolder)
{
File newFile = new File(file.ToJSON());
newFile.Title = newTitle;
// Set the new parent id.
if (newParentFolder != null)
newFile.Parents = new List<string> { newParentFolder.ID };
else
newFile.Parents = new List<string> { };
var duplicate = DuplicateFile(file, newFile);
while (duplicate.MoveNext())
yield return duplicate.Current;
}