2014-07-03 29 views
0

我是IOS開發新手,我在從mail.when下載附件時出現問題。當我試圖從郵件中下載圖像或PDF附件到使用MailCore2的文檔目錄時SDK下載成功。但我的問題是,下載的圖像或PDF文件沒有顯示任何東西(即)。它給了我文件,但有一個零字節的空文件。我能夠知道這個問題與我使用的NSDatawriteToFile是不正確的。但即時通訊無法解決它,請幫助我。無法從郵件成功下載圖像和PDF

這裏是我使用從郵件

下載文件中的代碼
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]; 
    } 
}]; 

請幫我擺脫這個問題,並告訴我,我在哪裏做的錯誤。

在此先感謝。

+0

有人幫我... –

+0

[msg.attachments objectAtIndex:0];是MCOA附件,而不是MCOIMAPPart。然後,您必須使用part.data而不是msg.data來獲取附件的內容。 –

回答

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]; 
 
      } 
 
     } 
 
    } 
 
}];

我希望他們是有用的。