Upload an image to Google Drive with OCR and convert to Doc

 ̄綄美尐妖づ 提交于 2021-02-10 22:21:49

问题


I want to upload an image to Google Drive with OCR function and convert it to Google Doc. I am using this method

public static File UploadFile(DriveService Service, string UploadFile, string Parent)
    {

        if (System.IO.File.Exists(UploadFile))
        {
            File body = new File();
            body.Title = System.IO.Path.GetFileName(UploadFile);
            body.Description = "File uploaded by Diamto Drive Sample";
            body.MimeType = GetMimeType(UploadFile);
            body.Parents = new List<ParentReference>() { new ParentReference() { Id = Parent } };

            // File’s content.
            byte[] byteArray = System.IO.File.ReadAllBytes(UploadFile);
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
            try
            {
                FilesResource.InsertMediaUpload request = Service.Files.Insert(body, stream, GetMimeType(UploadFile));
                request.Ocr = true;
                request.OcrLanguage = "he";
                request.Convert = true;
                return request.ResponseBody;
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
                return null;
            }
        }
        else
        {
            Console.WriteLine("File does not exist: " + UploadFile);
            return null;
        }

    }

and I call it via this line

Google.Apis.Drive.v2.Data.File newFile = DriveHelper.UploadFile(service, @"E:\myImage.png", myFolderId);

My problem is: request.ResponseBody returns null but when I remove this lines

 request.Ocr = true;
 request.OcrLanguage = "he";
 request.Convert = true;

My file uploaded successfully and request.ResponseBody returns File Object


回答1:


This is missing the Upload function.

request.Upload();
return request.ResponseBody;

Reference link: http://anthonygiretti.com/category/google-api/



来源:https://stackoverflow.com/questions/34582098/upload-an-image-to-google-drive-with-ocr-and-convert-to-doc

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