[英]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.