繁体   English   中英

无法成功从邮件下载图像和PDF

[英]Unable to download image and PDF from mail successfully

我是IOS开发的新手,在从mail下载附件时遇到问题。当我尝试使用MailCore2 SDK将邮件中的图像或PDF附件下载到文档目录时,下载成功。但是我的问题是下载的图像或PDF不显示任何内容(即)。它给我的文件是一个零字节的空文件.im能够知道NSData问题是我使用writeToFile是不正确的,但是我无法解决它。请帮我。

这是我用来从邮件下载文件的代码

MCOIMAPFetchContentOperation * op = [self.imapSession fetchMessageByUIDOperationWithFolder:@"INBOX" uid:[sender tag]];

[op start:^(NSError * error, NSData * data) {
    if ([error code] != MCOErrorNone) {
        return;
    }
    NSAssert(data != nil, @"data != nil");

    MCOMessageParser * msg = [MCOMessageParser messageParserWithData:data];

    if ([msg.attachments count] > 0)
    {
        MCOIMAPPart *part = [msg.attachments objectAtIndex:0];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *saveDirectory = [paths objectAtIndex:0];
        NSLog(@"%@",part.filename);

        NSString *attachmentPath = [[saveDirectory stringByAppendingString:@"/Downloads"] stringByAppendingPathComponent:part.filename];

        if (fileExists) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message"
                                                            message:@"Fail Existed in Download Path."
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        else{

            [msg.data writeToFile:attachmentPath atomically:YES];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message"
                                                            message:@"Download Success /n Saved in Downloads"
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        [self.tableView reloadData];
    }
}];

请帮助我摆脱这个问题,并告诉我我在哪里做错了。

提前致谢。

我曾经这样:

 MCOIMAPFetchContentOperation * op = [self.imapSession fetchMessageByUIDOperationWithFolder:@"INBOX" uid:[sender tag]]; [op start:^(NSError * error, NSData * data) { if ([error code] != MCOErrorNone) { return; } NSAssert(data != nil, @"data != nil"); MCOMessageParser * msg = [MCOMessageParser messageParserWithData:data]; if ([msg.attachments count] > 0) { MCOAttachment *attachment = [msg.attachments objectAtIndex:0]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *saveDirectory = [paths objectAtIndex:0]; NSLog(@"%@",attachment.filename); NSString *downLoadDirectory = [saveDirectory stringByAppendingString:@"/Downloads"]; if (![[NSFileManager defaultManager] fileExistsAtPath:downLoadDirectory]) { [[NSFileManager defaultManager] createDirectoryAtPath:downLoadDirectory withIntermediateDirectories:YES attributes:nil error:nil]; } NSString *attachmentPath = [downLoadDirectory stringByAppendingPathComponent:attachment.filename]; if ([[NSFileManager defaultManager] fileExistsAtPath:attachmentPath]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Fail Existed in Download Path." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } else { BOOL res = [[attachment data] writeToFile:attachmentPath atomically:YES]; if (res) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Download Success /n Saved in Downloads" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } } } }]; 

我希望它们是有用的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM