bomb上传文件到服务器

1、上传单个文件的方法

NSBundle *bundle = [NSBundle mainBundle];

NSString *path = [bundle pathForResource:@"title.plist" ofType:nil];

// NSString *fileString = [NSString stringWithFormat:@"%@/cs.txt" ,[bundle bundlePath] ];

BmobObject *obj = [[BmobObject alloc] initWithClassName:@"GameScore"];

BmobFile *file1 = [[BmobFile alloc] initWithFilePath:path];

[file1 saveInBackground:^(BOOL isSuccessful, NSError *error) {

NSLog(@"我走了");

//如果文件保存成功,则把文件添加到filetype列

if (isSuccessful) {

NSLog(@"成功了");

[obj setObject:file1 forKey:@"filetype"];

[obj saveInBackground];

//打印file文件的url地址

NSLog(@"file1 url %@",file1.url);

}else{

//进行处理

NSLog(@"%@",error);

}

}];

2、上传文件进度

在上传文件时,有时会需要获取上传文件进度的需求。这时,可以使用

-(void)saveInBackground:(BmobBooleanResultBlock)block withProgressBlock:(BmobProgressBlock)progressBlock;

如在下面的例子中,打印上传的进度

NSBundle    *bundle = [NSBundle mainBundle];

NSString *fileString = [NSString stringWithFormat:@"%@/cs.txt" ,[bundle bundlePath] ];

BmobObject *obj = [[BmobObject alloc] initWithClassName:@"gameScoreFile"];

BmobFile *file1 = [[BmobFile alloc] initWithClassName:@"Asc" withFilePath:fileString];

[file1 saveInBackground:^(BOOL isSuccessful, NSError *error) {

if (isSuccessful) {

[obj setObject:file1  forKey:@"userFile"];

[obj saveInBackground];

NSLog(@"file1 url %@",file1.url);

}

} withProgressBlock:^(CGFloat progress) {

NSLog(@"上传进度%.2f",progress);

}];

3、以分片的方式上传文件

分片上传文件和上传整个文件的机制有所不同,是先把整个文件进行分片(256KB一片),然后再进行一片一片的上传(具体实现可查看RestAPI文档)。当文件以分片的方式上传到Bmob服务器时,具有几种优势:

适合于尺寸较大的文件传输,通过切片来避免单个HTTP数据量过大而导致连接超时;

在网络条件较差的环境下,较小的尺寸可以有较高的上传成功率,从而避免无休止的失败重试;

在BmobSDK中对应的函数方法为

-(void)saveInBackgroundByDataSharding:(BmobBooleanResultBlock)block;

示例如下:

NSBundle    *bundle = [NSBundle mainBundle];

//上传cs.txt文件

NSString *fileString = [NSString stringWithFormat:@"%@/cs.txt" ,[bundle bundlePath] ];

BmobObject *obj = [[BmobObject alloc] initWithClassName:@"gameScoreFile"];

//创建BmobFile对象

BmobFile *file1 = [[BmobFile alloc] initWithFilePath:fileString];

[file1 saveInBackgroundByDataSharding:^(BOOL isSuccessful, NSError *error) {

if (isSuccessful) {

//如果成功,保存文件到userFile

[obj setObject:file1  forKey:@"userFile"];

[obj saveInBackground];

}else{

//失败,打印错误信息

NSLog(@"error: %@",[error description]);

}

} ];

4、批量上传文件

有时,开发者需要一次性上传多个文件,这是可以使用SDK提供的多个上传文件的方法来使用

NSBundle    *bundle = [NSBundle mainBundle];

//文件cncc.jpg的路径

NSString *fileString = [NSString stringWithFormat:@"%@/cncc.jpg" ,[bundle bundlePath] ];

//文件cs.txt的路径

NSString *fileString2 = [NSString stringWithFormat:@"%@/cs.txt" ,[bundle bundlePath] ];

[BmobFile filesUploadBatchWithPaths:@[fileString,fileString2]

progressBlock:^(int index, float progress) {

//index 上传数组的下标,progress当前文件的进度

NSLog(@"index %d progress %f",index,progress);

} resultBlock:^(NSArray *array, BOOL isSuccessful, NSError *error) {

//array 文件数组,isSuccessful 成功或者失败,error 错误信息

BmobObject *obj = [[BmobObject alloc] initWithClassName:@"gameScoreFile"];

for (int i = 0 ; i < array.count ;i ++) {

BmobFile *file = array [i];

NSString *key = [NSString stringWithFormat:@"userFile%d",i];

[obj setObject:file  forKey:key];

}

[obj saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {

}];

}];

5、下载文件

获取文件对象只需通过-(id)objectForKey:(id)key;来得到,例如,

BmobFile *file = (BmobFile*)[gameScore objectForKey:@"filetype"];

可用通过file的url属性(file.url),来得到文件的地址进行下载。


//查找User表所有信息

    BmobQuery   *musiC = [BmobQuery queryWithClassName:@"Music"];

    //查找GameScore表的数据

    [musiC findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {

        infos = [NSMutableArray arrayWithArray:array];

        

       

        for (BmobObject *obj in array) {

            //打印playerName

            NSLog(@"obj.music_name = %@",     [obj objectForKey:@"music_name"]);

            BmobFile *file = [obj objectForKey:@"music_file"];

            NSLog(@"obj.music_file = %@",    file.url);

            BmobFile *pic = [obj objectForKey:@"music_pic"];

            NSLog(@"obj.music_pic = %@",    pic.url);

            NSLog(@"----------------------分割线----------------------------");


6、删除文件

删除文件接口只能删除1.6.9版本之后上传的文件

如果需要删除文件,使用以下接口即可

/**

*  异步请求删除文件

*

*  @param block 返回删除结果与信息,如果删除成功,则无返回信息

*/

-(void)deleteInBackground:(BmobBooleanResultBlock)block;

当开发者需要一次性删除多个文件的时候,可以调用批量删除文件的接口

NSArray *array = @[@"http://bmob-cdn-1.b0.upaiyun.com/jpg/579c8dc6676e460b82d83c8eb5c8aaa5.jpg",@"http://bmob-cdn-1.b0.upaiyun.com/jpg/59e3817d6cec416ba99a126c9d42768f.jpg "]

[BmobFile filesDeleteBatchWithArray:array resultBlock:^(NSArray *array, BOOL isSuccessful, NSError *error) {

NSLog(@"fail delete array %@",array);

NSLog(@"error %@",error.localizedDescription);

NSLog(@"issuccessful %i",isSuccessful);

}];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值