Reputation: 6566
I am generating reports resulting in WORD files using xdocreport.
From the generated report, I create a InputStreamContent
with MIME-TYPE "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
(MS WORD - DOCX) to write to Google Drive :
// create Word file stream using xdocreport
OutputStream2InputStream outputStream = new OutputStream2InputStream(); // buffer
report.process(context, outputStream);
// create inputstream for Google Drive
InputStreamContent inputStream = new InputStreamContent("application/vnd.openxmlformats-officedocument.wordprocessingml.document",
outputStream.getInputStream());
inputStream.setLength(outputStream.size());
WRITING MSWORD DOCUMENT WORKS FINE (CONVERT= FALSE) :
File file = new File();
Insert insertOperation = service.files().insert(file, inputStream).setConvert(false);
file.setTitle("test.docx");
file.setMimeType(inputstream.getType());
File result = insertOperation.execute();
Resulting in a WORD DOCX file created on my Google Drive.
WRITING SAME INPUTSTREAM WITH CONVERT=TRUE FAILS
File file = new File();
Insert insertOperation = service.files().insert(file, inputStream).setConvert(true);
file.setTitle("test");
//file.setMimeType(inputstream.getType()); // what here ?
File result = insertOperation.execute();
RESULT
1. When NOT setting the mime type : newly created File result
has 0 bytes and MIME-type: application/vnd.google-apps.kix
2. When setting the mime type : MIME-TYPE set to "application/vnd.google-apps.document" and convert = true, results in 400: BAD REQUEST.
What am I doing wrong ?
Upvotes: 1
Views: 1130
Reputation: 5404
All what you need to do is to update your getType() with the correct .docx MIME-type.
docx=> application/vnd.openxmlformats-officedocument.wordprocessingml.document
I had the same issue and this piece of code fixed it!
Upvotes: 0
Reputation: 41643
This is a common problem. Don't set a MIME type in the request metadata. Google Drive will decide the MIME type to convert to.
Your line marked // what here ?
should be left out.
Upvotes: 1