我是IOS开发的新手,我在从邮件下载附件时遇到问题。当我尝试使用MailCore2 SDK将图像或PDF附件从我的邮件下载到文档目录时,它正在成功下载。但是我的问题是下载的图像或PDF没有显示任何内容(即)。它给我文件,但文件为零,空文件为零。我能够知道问题是NSData
我使用{{1} }是不正确的。但我无法解决它。请帮助我。
以下是我用来从邮件下载文件的代码
writeToFile
请帮我解决这个问题并告诉我我在哪里做错了。
提前致谢。
答案 0 :(得分:0)
我用过这样的话:
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];
}
}
}
}];

我希望它们有用。