使用goldraccoon ftp-upload损坏图像和pdf

时间:2015-01-22 07:49:17

标签: ios objective-c xcode ftp

我在iOS-Application中使用GoldRaccoon进行ftp-upload。 filetransfer使用txt文件进行perfekt,文件大小可达mb。但是图片和pdf文件在上传时会被破坏。

没有错误,上传似乎是成功的。

当我在应用程序的文件夹(使用iExplorer)中查看iPad时,文件很好。它也等于图像大小的大小。如果它是jpg或png,它也是相同的。

当我开始上传时,所有文件都存在。

- (void)uploadFilesToFTP:(Objekt *)objekt withCsvFilePath:(NSString *)csvLocalFilePath {


__block typeof(self) bself = self;

[self.library enumerateGroupsWithTypes:ALAssetsGroupAlbum
                            usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                                if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:self.albumName]) {
                                    InfoLogMy(@"found album %@", self.albumName);
                                    self.groupToAddTo = group;
                                }
                            }
                          failureBlock:^(NSError* error) {

                              UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Fehler" message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
                              [alertView show];
                              return;
                          }];


// dateien
InfoLogMy("Count of files %d", objekt.dateien.count);
for (Datei *datei in objekt.dateien)
{
    InfoLogMy(@"Upload starts");

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dateiPath = [documentsDirectory stringByAppendingPathComponent:datei.dateiname];
    NSError *error;
    NSData *data = [[NSData alloc] initWithContentsOfFile:dateiPath options:NSDataReadingUncached error:&error];
    if( [[NSFileManager defaultManager] fileExistsAtPath:dateiPath] )
    {
        if (!error) {

            if (datei.zimmer != nil) {

                // original image
                UIImage *originalImage = [UIImage imageWithData:data];

                __block typeof(self) bself = self;
                //Bild wird in das Photoalbum des iPads als Sicherung abgelegt.
                //Dies ist nicht mit PDF-Dokumenten möglich

                [self.library writeImageToSavedPhotosAlbum:[originalImage CGImage]
                                                  metadata:nil
                                           completionBlock:^(NSURL* assetURL, NSError* error) {
                                               if (error.code == 0) {

                                                   // try to get the asset
                                                   [self.library assetForURL:assetURL
                                                                 resultBlock:^(ALAsset *asset) {

                                                                     // assign the photo to the album
                                                                     [bself.groupToAddTo addAsset:asset];
                                                                 }
                                                                failureBlock:^(NSError* error) {
                                                                    InfoLogMy("failed to retrieve image asset:\nError: %@ ", [error localizedDescription]);
                                                                }];
                                               }
                                               else {
                                                   InfoLogMy("saved image failed.\nerror code %i\n%@", error.code, [error localizedDescription]);
                                               }
                                           }];
            }

            // copy of image
            NSString *filenameCopy = [NSString stringWithFormat:@"copy-%@", datei.dateiname];
            NSString *localFilePathCopy = [documentsDirectory stringByAppendingPathComponent:filenameCopy];

            BOOL copyFileExists = [[NSFileManager defaultManager] fileExistsAtPath:localFilePathCopy];

            BOOL hasPaths = ([[datei.zeichnungen allObjects] count] > 0);
            BOOL hasSymbols = ([[datei.symbole allObjects] count] > 0);

            NSString *fileExtension = [datei.dateiname substringWithRange:NSMakeRange(datei.dateiname.length - 3, 3)];

            BOOL isPDF = ([fileExtension isEqualToString:@"pdf"]);

            if (copyFileExists == YES && (hasPaths == YES || hasSymbols == YES|| isPDF == YES)) {
                NSError *error;
                NSData *dataCopy = [[NSData alloc] initWithContentsOfFile:localFilePathCopy options:NSDataReadingUncached error:&error];

                if (isPDF == NO) {
                    UIImage *copyImage = [UIImage imageWithData:dataCopy];

                    [self.library writeImageToSavedPhotosAlbum:[copyImage CGImage]
                                                      metadata:nil
                                               completionBlock:^(NSURL* assetURL, NSError* error) {
                                                   if (error.code == 0) {
                                                       InfoLogMy(@"saved image completed:\nurl: %@", assetURL);

                                                       // try to get the asset
                                                       [self.library assetForURL:assetURL
                                                                     resultBlock:^(ALAsset *asset) {

                                                                         // assign the photo to the album
                                                                         [bself.groupToAddTo addAsset:asset];
                                                                     }
                                                                    failureBlock:^(NSError* error) {
                                                                        InfoLogMy(@"failed to retrieve image asset:\nError: %@ ", [error localizedDescription]);
                                                                    }];
                                                   }
                                                   else {
                                                       InfoLogMy(@"saved image failed.\nerror code %i\n%@", error.code, [error localizedDescription]);
                                                   }
                                               }];

                }


                if (!error) {

                    [self.requestsManager addRequestForUploadFileAtLocalPath:localFilePathCopy toRemotePath:[NSString stringWithFormat:@"%@%@", kFtpPathOutbox, filenameCopy]];

                }
            }
            else
            {

                //Wenn die Datei keine Zuordnung zu einem Zimmer hat.
                if (datei.zimmer != nil)
                {
                    [self.requestsManager addRequestForUploadFileAtLocalPath:dateiPath toRemotePath:[NSString stringWithFormat:@"%@%@", kFtpPathOutbox, datei.dateiname]];
                }
            }

        } else {
            // TO DO: Alert mit Fehler
            return;
        }

    }
    else
    {
        InfoLogMy(@"Datei existiert leider nicht");
        [Mbs writeLog:[NSString stringWithFormat:@"Datei existiert nicht (%@)", datei.dateiname]];
    }
}

NSArray *parts = [csvLocalFilePath componentsSeparatedByString:@"/"];

NSString *remotePath = [NSString stringWithFormat:@"%@%@", kFtpPathOutbox, [parts lastObject]];

[self.requestsManager addRequestForUploadFileAtLocalPath:csvLocalFilePath toRemotePath:remotePath];

self.requestsManagerFailed = NO;

[self.requestsManager startProcessingRequests];
}

0 个答案:

没有答案