问题
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