谷歌文档上传问题iphone(GData API)

时间:2012-03-14 14:35:22

标签: iphone

我正在研究用于iphone的示例谷歌文档应用程序,其中我成功从谷歌获取文档,但是为了通过谷歌文档上传文档,我遇到了一个问题:请求URL时(NSURL * urlPost = [[self.feedDocList] postLink] URL];)我从GTLink类对象获取nil值。所以我无法将文件上传到谷歌文档。 我正在使用以下代码。

           GDataFeedDocList *feedDocList;   
    Class classEntry = nil;

    GDataEntryDocBase *entryNew = [classEntry documentEntry];
    [entryNew setTitleWithString:self.title];// file title
    [entryNew setUploadData:self.dataToUpload]; // binary data
    [entryNew setUploadMIMEType:typeMime]; mime type
    [entryNew setUploadSlug:self.title];  // title              
            NSURL *urlPost=[[self.feedDocList postLink] URL];
            DebugLog(@"urlPost = %@", urlPost);

    self.ticketUpload = [serviceDocs fetchDocEntryByInsertingEntry:entryNew
               forFeedURL:urlPost delegate:self            didFinishSelector:@selector(uploadFileTicket:finishedWithEntry:)                didFailSelector:@selector(uploadFileTicket:failedWithError:)];

1 个答案:

答案 0 :(得分:3)

正如您所提到的,您可以从GDoc中检索数据,这意味着您已完成身份验证部分。所以要将文件上传到Google Doc,请使用以下代码。

NSString *errorMsg = nil;

// make a new entry for the file

NSString *mimeType = nil;
Class entryClass = nil;

NSString *extn = [path pathExtension];
[self getMIMEType:&mimeType andEntryClass:&entryClass forExtension:extn];

if (!mimeType) 
{
    // for other file types, see if we can get the type from the Mac OS
    // and use a generic file document entry class
    mimeType = [GDataUtilities MIMETypeForFileAtPath:path
                                     defaultMIMEType:nil];
    entryClass = [GDataEntryFileDoc class];
}

if (!mimeType) 
{
    errorMsg = [NSString stringWithFormat:@"need MIME type for file %@", path];
}

if (mimeType && entryClass) 
{
    GDataEntryDocBase *newEntry = [entryClass documentEntry];

    NSString *title = [[NSFileManager defaultManager] displayNameAtPath:path];
    [newEntry setTitleWithString:title];

    NSFileHandle *uploadFileHandle = [NSFileHandle fileHandleForReadingAtPath:path];

    if (!uploadFileHandle) 
    {
        errorMsg = [NSString stringWithFormat:@"cannot read file %@", path];
    }

    if (uploadFileHandle) 
    {
        [newEntry setUploadFileHandle:uploadFileHandle];
        [newEntry setUploadData:[NSData dataWithContentsOfFile:path]]; 
        [newEntry setUploadMIMEType:mimeType];
        [newEntry setUploadSlug:[path lastPathComponent]];

        NSURL *uploadURL = [GDataServiceGoogleDocs docsUploadURL];

        GDataServiceGoogleDocs *service = [self docsService];

        [service setAuthorizer:self.authentication];

        GDataServiceTicket *ticket;

        ticket = [service fetchEntryByInsertingEntry:newEntry
                                          forFeedURL:uploadURL
                                            delegate:self
                                   didFinishSelector:@selector(uploadFileTicket:finishedWithEntry:error:)];

        [self setUploadTicket:ticket];
    }
}

if (errorMsg) 
{
    [self displayAlert:@"Upload Error"
                format:@"%@", errorMsg];

    NSLog(@"%@", errorMsg);

}

Mime Type的方法。

  - (void)getMIMEType:(NSString **)mimeType andEntryClass:(Class *)class forExtension:(NSString *)extension {

// Mac OS X's UTI database doesn't know MIME types for .doc and .xls
// so GDataEntryBase's MIMETypeForFileAtPath method isn't helpful here

struct MapEntry {
    NSString *extension;
    NSString *mimeType;
    NSString *className;
};

static struct MapEntry sMap[] = {
    { @"csv", @"text/csv", @"GDataEntryStandardDoc" },
    { @"doc", @"application/msword", @"GDataEntryStandardDoc" },
    { @"docx", @"application/vnd.openxmlformats-officedocument.wordprocessingml.document", @"GDataEntryStandardDoc" },
    { @"ods", @"application/vnd.oasis.opendocument.spreadsheet", @"GDataEntrySpreadsheetDoc" },
    { @"odt", @"application/vnd.oasis.opendocument.text", @"GDataEntryStandardDoc" },
    { @"pps", @"application/vnd.ms-powerpoint", @"GDataEntryPresentationDoc" },
    { @"ppt", @"application/vnd.ms-powerpoint", @"GDataEntryPresentationDoc" },
    { @"rtf", @"application/rtf", @"GDataEntryStandardDoc" },
    { @"sxw", @"application/vnd.sun.xml.writer", @"GDataEntryStandardDoc" },
    { @"txt", @"text/plain", @"GDataEntryStandardDoc" },
    { @"xls", @"application/vnd.ms-excel", @"GDataEntrySpreadsheetDoc" },
    { @"xlsx", @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", @"GDataEntrySpreadsheetDoc" },
    { @"jpg", @"image/jpeg", @"GDataEntryStandardDoc" },
    { @"jpeg", @"image/jpeg", @"GDataEntryStandardDoc" },
    { @"png", @"image/png", @"GDataEntryStandardDoc" },
    { @"bmp", @"image/bmp", @"GDataEntryStandardDoc" },
    { @"gif", @"image/gif", @"GDataEntryStandardDoc" },
    { @"html", @"text/html", @"GDataEntryStandardDoc" },
    { @"htm", @"text/html", @"GDataEntryStandardDoc" },
    { @"tsv", @"text/tab-separated-values", @"GDataEntryStandardDoc" },
    { @"tab", @"text/tab-separated-values", @"GDataEntryStandardDoc" },
    { @"pdf", @"application/pdf", @"GDataEntryPDFDoc" },
    { nil, nil, nil }
};

NSString *lowerExtn = [extension lowercaseString];

for (int idx = 0; sMap[idx].extension != nil; idx++) {
    if ([lowerExtn isEqual:sMap[idx].extension]) {
        *mimeType = sMap[idx].mimeType;
        *class = NSClassFromString(sMap[idx].className);
        return;
    }
}

*mimeType = nil;
*class = nil;
return;

}

如果您正在关注gDoc示例代码。然后在上面的代码的帮助下,您将能够将文件上传到Gdoc。最好的运气。