Google Drive Parse error when uploading file with special unicode characters

混江龙づ霸主 提交于 2019-12-11 17:58:02

问题


A problem has occurred recently when trying to upload files with the Google API dotnet client sdk. When the file name has any special Unicode characters, it throws an error.

Here is my code

    public static Google.Apis.Drive.v2.Data.File InsertResource(Google.Apis.Drive.v2.DriveService service, string filePath, string parentId, string fileName, string mimeType)
    {
        Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();

        try
        {
            // File's metadata.

            body.Title = fileName;
            body.MimeType = mimeType;

            // Set the parent folder.
            if (!String.IsNullOrEmpty(parentId))
            {
                var response = DriveUtils.GetFileInfo(service, parentId);
                if (response.Error != null)
                {
                    body.Error = response.Error;
                    return body;
                }
                body.Parents = new List<ParentReference>() { new ParentReference() { Id = parentId } };
            }

            byte[] byteArray = System.IO.File.ReadAllBytes(filePath);
            MemoryStream stream = new MemoryStream(byteArray);

            Google.Apis.Drive.v2.FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);

            request.Upload();
            return request.ResponseBody;

        }
        catch (GoogleApiRequestException e)
        {
            body.Error = e.RequestError;
            return body;
        }
}

It was working fine up until this week. Any files that have chinese characters or turkish characters in the name will throw an error.

at Google.Apis.Json.JsonReader.ParseExpression(JsonToken token, TokenStream ts) at Google.Apis.Json.JsonReader.Parse(String jsonAsText) at Google.Apis.Upload.ResumableUpload`1.Upload()


回答1:


Try to download the latest version of the API (from https://code.google.com/p/google-api-dotnet-client/wiki/APIs#Drive_API). I changed line 122 in the Drive sample (instructions to download this sample are here), to Title = "字/漢字" and Title = "title with ç", and both of them worked for me.



来源:https://stackoverflow.com/questions/17436002/google-drive-parse-error-when-uploading-file-with-special-unicode-characters

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