OGeek|极客世界-中国程序员成长平台

标题: ios - 为上传的内容(Google Drive、iOS)提供了无效的 MIME 类型 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:38
标题: ios - 为上传的内容(Google Drive、iOS)提供了无效的 MIME 类型

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

我的项目信息:

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"}



Best Answer-推荐答案


对我有用的是在上传参数中设置“源”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 - 为上传的内容(Google Drive、iOS)提供了无效的 MIME 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39694639/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4