将多个文件上传到单行parse.com

时间:2014-08-03 16:47:27

标签: objective-c parse-platform

所以我使用以下代码将多个文件上传到一个类的一行。

for (NSString* currentString in directoryContents){

    NSLog(@"%@",currentString);
    NSString *temp2 = [temp stringByAppendingPathComponent:currentString];
    PFFile *file = [PFFile  fileWithName:currentString contentsAtPath:temp2];
    [file saveInBackground];
    PFObject *obj = [PFObject objectWithClassName:@"DreamBits"];

    if ([currentString isEqualToString:@"index.html"]) {
        [obj setObject:file forKey:@"index"];
    }
    else {
        count += 1;

        NSString *filen = [NSString stringWithFormat:@"file%i",count];
        NSLog(@"%@",filen);

        [obj setObject:file forKey:filen];
    }
    [obj saveInBackground];
}

问题是由于某种原因我得到了the files in different rows。知道怎么纠正这个吗?

2 个答案:

答案 0 :(得分:0)

我修改了你的代码。我没有运行此代码,但希望它可以帮助您。

PFObject *obj = [PFObject objectWithClassName:@"DreamBits"];

    for (NSString* currentString in directoryContents){

        NSLog(@"%@",currentString);
        NSString *temp2 = [temp stringByAppendingPathComponent:currentString];
        PFFile *file = [PFFile  fileWithName:currentString contentsAtPath:temp2];

            if ([currentString isEqualToString:@"index.html"]) {
                [obj setObject:file forKey:@"index"];
            }
            else {
                count += 1;

                NSString *filen = [NSString stringWithFormat:@"file%i",count];
                NSLog(@"%@",filen);

                [obj setObject:file forKey:filen];
            }
    }

    [obj saveInBackground];

在循环外创建PFObject。将所有PFFile对象放入循环内的PFObject。

循环后,保存PFObject。最好使用该方法:

        [obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
        {
               //Check whether the upload is succeeded or not
        }];

答案 1 :(得分:0)

我已经使用此方法在Parse Server表中的一行中上传配置文件和缩略图。使用swift 4和Parse SDK 1.17.1。希望这项技术会有所帮助。

   func uploadImage(profileImage: UIImage, profileImageName: String, thumbImage: UIImage, thumbImageName: String) {

    if let profileImagefile = PFFile.init(name: profileImageName, data: profileImage.jpegData(compressionQuality: 1)!) {
        let fileObject = PFObject(className:"ProfileImage")
        fileObject.setValue(profileImagefile, forKey: "profile_image")
        fileObject.saveInBackground { (success, error) in
            if error == nil {
                print("thumb image path: \(profileImagefile.url ?? "")")
                if let thumbImage = PFFile.init(name: thumbImageName, data: thumbImage.jpegData(compressionQuality: 0.5)!) {
                    fileObject.setValue(thumbImage, forKey: "thumb_image")
                    fileObject.saveInBackground(block: { (result, fileError) in
                        if fileError == nil {
                            print("thumb image path: \(thumbImage.url ?? "")")
                        }else {
                            print("error on thumb upload: \(result)")
                        }
                    })
                }
            }else {
                print("error on file upload: \(error.debugDescription)")
            }
        }
    }
}