上传 Excel 文件 Google 驱动器 API
Upload Excel file Google Drive API
我正在使用 Google 驱动器 API 上传 Excel 文件。我正确地进行了 API 连接。创建一个文件夹并正确上传 excel 文件。我可以在我的 google 驱动器中看到它们。但是,excel 文件是空的。我在这里缺少什么?
我的代码如下:
$path = "uploads/school_info.xlsx"; //file that I am uploading, inside a folder of my website.
$folder_id = create_folder("School_".$_POST['submit_uid'], "****folder-id****");
$file_name = "School 1";
$success = insert_file_to_drive( $path, $file_name, $folder_id);
// This will insert file into drive and returns boolean values.
function insert_file_to_drive( $file_path, $file_name, $parent_file_id = null ) {
$service = new Google_Service_Drive( $GLOBALS['client'] );
$file = new Google_Service_Drive_DriveFile();
$file->setName( $file_name );
if( !empty( $parent_file_id ) ){
$file->setParents( [ $parent_file_id ] );
}
$result = $service->files->create(
$file,
array(
'data' => file_get_contents($file_path),
//'mimeType' => 'application/octet-stream',
'mimeType' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', //works but still empty excel
)
);
$is_success = false;
if( isset( $result['name'] ) && !empty( $result['name'] ) ){
$is_success = true;
}
return $is_success;
}
回答
我已使用以下代码成功上传 .xlsx
文件。显然它与你的非常相似,我只是更改了 mimeType
并添加了 uploadType
。文件上传后,我可以从 UI 版本的云端硬盘访问和查看文件内容。如果仍然失败,请通过从 here.
下载示例文件来重复该过程
代码
$service = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$file->setName('test.xlsx');
$data = file_get_contents('file_example_XLSX_50.xlsx');
$result = $service->files->create($file, array(
'data' => $data,
'mimeType' => 'application/vnd.ms-excel',
'uploadType' => 'multipart'
));
好的,我找到问题所在了。正如我在上面的代码中所写,我有一个相对路径
$path = "uploads/school_info.xlsx";
当我将其更改为这样的完整路径时:
$path = "https://www.example.com/uploads/school_info.xlsx";
一切正常
谢谢大家的宝贵时间
我正在使用 Google 驱动器 API 上传 Excel 文件。我正确地进行了 API 连接。创建一个文件夹并正确上传 excel 文件。我可以在我的 google 驱动器中看到它们。但是,excel 文件是空的。我在这里缺少什么?
我的代码如下:
$path = "uploads/school_info.xlsx"; //file that I am uploading, inside a folder of my website.
$folder_id = create_folder("School_".$_POST['submit_uid'], "****folder-id****");
$file_name = "School 1";
$success = insert_file_to_drive( $path, $file_name, $folder_id);
// This will insert file into drive and returns boolean values.
function insert_file_to_drive( $file_path, $file_name, $parent_file_id = null ) {
$service = new Google_Service_Drive( $GLOBALS['client'] );
$file = new Google_Service_Drive_DriveFile();
$file->setName( $file_name );
if( !empty( $parent_file_id ) ){
$file->setParents( [ $parent_file_id ] );
}
$result = $service->files->create(
$file,
array(
'data' => file_get_contents($file_path),
//'mimeType' => 'application/octet-stream',
'mimeType' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', //works but still empty excel
)
);
$is_success = false;
if( isset( $result['name'] ) && !empty( $result['name'] ) ){
$is_success = true;
}
return $is_success;
}
回答
我已使用以下代码成功上传 .xlsx
文件。显然它与你的非常相似,我只是更改了 mimeType
并添加了 uploadType
。文件上传后,我可以从 UI 版本的云端硬盘访问和查看文件内容。如果仍然失败,请通过从 here.
代码
$service = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$file->setName('test.xlsx');
$data = file_get_contents('file_example_XLSX_50.xlsx');
$result = $service->files->create($file, array(
'data' => $data,
'mimeType' => 'application/vnd.ms-excel',
'uploadType' => 'multipart'
));
好的,我找到问题所在了。正如我在上面的代码中所写,我有一个相对路径
$path = "uploads/school_info.xlsx";
当我将其更改为这样的完整路径时:
$path = "https://www.example.com/uploads/school_info.xlsx";
一切正常
谢谢大家的宝贵时间