ios - 为上传的内容提供的 MIME 类型无效(Google 云端硬盘、iOS)

标签 ios objective-c csv google-drive-api

我正在尝试使用 files.insert 创建一个 CSV 文件到 Google Drive .不幸的是,我有两个问题。请帮助我解决它。

我的项目信息:

  • 语言:Objective-C

  • Pod:GoogleAPIClientForREST/Drive

1/我在框架中找不到 files.insert。所以,我使用 files.create。我说得对吗?

2/

Accepted Media MIME types: */*

当我使用 mimeType: 'text/csv' 时它工作正常。但它看起来像这样:

CSV-file in google Drive with 'text/csv'

而且,我需要这样的东西来确保客户的要求和易于编辑

CSV-file in google Drive with 'application/vnd.google-apps.spreadsheet'

因此,我将 mimeType 更改为“application/vnd.google-apps.spreadsheet”。这让我犯了一个错误:

An error occurred: Error Domain=com.google.GTLRErrorObjectDomain Code=400 "(Invalid MIME type provided for the uploaded content.)" UserInfo={error=Invalid MIME type provided for the uploaded content., GTLRStructuredError=GTLRErrorObject 0x7fc77c808840: {message:"Invalid MIME type provided for the uploaded content." errors:[1] code:400}, NSLocalizedFailureReason=(Invalid MIME type provided for the uploaded content.)}

这是我的代码:

GTLRDrive_File *metadata = [GTLRDrive_File object];
metadata.name = @"export.csv";
NSString *content = @"a,b,c\nx,y,z";
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
GTLRUploadParameters *uploadParameters = [GTLRUploadParameters uploadParametersWithData:data MIMEType:@"application/vnd.google-apps.spreadsheet"];

GTLRDriveQuery_FilesCreate *query = [GTLRDriveQuery_FilesCreate queryWithObject:metadata
                                                   uploadParameters:uploadParameters];

[self.service executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,
                                                     GTLRDrive_File *updatedFile,
                                                     NSError *error) {
}];

这段代码有时可以与我的谷歌帐户一起使用,并且 GTLRDriveQuery_FilesUpdate 运行得非常好!

File GTLRDrive_File 0x7fee0587b970: {mimeType:"application/vnd.google-apps.spreadsheet" id:"..." kind:"drive#file" name:"export.csv"}

最佳答案

对我有用的是在上传参数中设置“源”mime 类型,并在文件对象中设置“目标”mime 类型:

NSData *data = [csv dataUsingEncoding: NSUTF8StringEncoding];
NSString *mimeType = @"text/csv";
NSString *convertToMimeType = @"application/vnd.google-apps.spreadsheet";


GTLRDrive_File *newFile = [GTLRDrive_File object];
newFile.name = fileName;
if (convertToMimeType != nil) newFile.mimeType = convertToMimeType;
if (parentItem != nil) newFile.parents = [NSArray arrayWithObject: parentItem.identifier];
GTLRUploadParameters *uploadParameters = [GTLRUploadParameters uploadParametersWithData: data MIMEType: mimeType];
GTLRDriveQuery_FilesCreate *query = [GTLRDriveQuery_FilesCreate queryWithObject: newFile uploadParameters: uploadParameters];

关于ios - 为上传的内容提供的 MIME 类型无效(Google 云端硬盘、iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39694639/

相关文章:

ios - 在 UITableView 中设置显示当前项目的菜单

ios - 如何从 iOS-Objective-c 中的联系人框架获取生日和周年纪念标签

ios - 如何以编程方式更改 UITableView 的类?

performance - Powershell 脚本的速度。寻求优化

ios - 核心数据 : How to display results of a fetchrequest for calculating values in a tableview cell

ios - 如何在 Swift 中实现异步/等待?

ios - 如何将相机外部矩阵转换为 SCNCamera 位置和旋转

iphone - UITextView 中光标的像素位置

python - 如何在 Python 中向日期时间对象添加填充零?

r - Shiny 的应用程序中的条件边栏取决于所选的选项卡