├── QiniuManager.h ├── QiniuManager.m └── README.md /QiniuManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // QiniuManager.h 3 | // awesomeMobile 4 | // 5 | // Created by scott on 15/12/17. 6 | // Copyright © 2015年 Facebook. All rights reserved. 7 | // 8 | #import 9 | #import "QiniuSDK.h" 10 | #import "RCTBridgeModule.h" 11 | 12 | @interface QiniuManager : NSObject 13 | @property (nonatomic, retain) QNUploadOption *opt; 14 | @end 15 | -------------------------------------------------------------------------------- /QiniuManager.m: -------------------------------------------------------------------------------- 1 | // 2 | // QiniuManager.m 3 | // awesomeMobile 4 | // 5 | // Created by scott on 15/12/17. 6 | // Copyright © 2015年 Facebook. All rights reserved. 7 | // 8 | 9 | #import "QiniuManager.h" 10 | #import "RCTBridge.h" 11 | 12 | @implementation QiniuManager 13 | 14 | - (NSString *)mimeTypeForData:(NSData *)data { 15 | uint8_t c; 16 | [data getBytes:&c length:1]; 17 | 18 | switch (c) { 19 | case 0xFF: 20 | return @"image/jpeg"; 21 | break; 22 | case 0x89: 23 | return @"image/png"; 24 | break; 25 | case 0x47: 26 | return @"image/gif"; 27 | break; 28 | case 0x49: 29 | case 0x4D: 30 | return @"image/tiff"; 31 | break; 32 | case 0x25: 33 | return @"application/pdf"; 34 | break; 35 | case 0xD0: 36 | return @"application/vnd"; 37 | break; 38 | case 0x46: 39 | return @"text/plain"; 40 | break; 41 | default: 42 | return @"application/octet-stream"; 43 | } 44 | return nil; 45 | } 46 | 47 | RCT_EXPORT_MODULE(); 48 | 49 | 50 | RCT_EXPORT_METHOD(uploadToQiniu:(NSString *)uri 51 | key:(NSString *)key 52 | token:(NSString *)token 53 | params:(NSDictionary *)params 54 | callback:(RCTResponseSenderBlock)callback) 55 | { 56 | // QNUploadManager *upManager = [[QNUploadManager alloc] init]; 57 | 58 | QNUploadManager *upManager = [QNUploadManager sharedInstanceWithConfiguration:nil]; 59 | 60 | // NSData *imageData = [NSData dataWithContentsOfFile: finalPath]; 61 | NSError *error = nil; 62 | 63 | NSData *data = [NSData dataWithContentsOfFile: uri options:0 error:&error]; 64 | 65 | NSString *mimeString = [self mimeTypeForData:data]; 66 | NSMutableDictionary *mutableDict = [params mutableCopy]; 67 | [mutableDict setObject:mimeString forKey:@"x:mimeType"]; 68 | params = [mutableDict mutableCopy]; 69 | // NSLog(@"----params: %@", params); 70 | 71 | self.opt = [[QNUploadOption alloc] initWithMime:mimeString progressHandler:nil params:params checkCrc:YES cancellationSignal:nil]; 72 | // NSLog(@"------qiniu opt: %@", self.opt); 73 | [upManager putData:data key:key token:token 74 | complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) { 75 | // NSLog(@"-----update callback params:%@", resp); 76 | if (resp == nil) { 77 | // NSDictionary *res = [NSDictionary]; 78 | NSDictionary *res = @{@"status":@500, @"message": @"上传失败"}; 79 | callback(@[res]); 80 | }else{ 81 | callback(@[resp]); 82 | } 83 | 84 | 85 | } option: self.opt]; 86 | 87 | } 88 | 89 | @end 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-qiniu 2 | react native qiniu 3 | 4 | ### useage: 5 | 6 | add `QiniuManager.h` & `QiniuManager.m` to your project, and then in your react-native code use it like this: 7 | 8 | 9 | ``` 10 | import React, { NativeModules } from 'react-native' 11 | 12 | var QiniuManager = NativeModules.QiniuManager; 13 | 14 | QiniuManager.uploadToQiniu(uri,key,token,params,function(result){ 15 | if (!result) { 16 | console.log('上传图片失败,请稍后再试'); 17 | return; 18 | }; 19 | // self.alert('上传图片成功'); 20 | callback(result); 21 | }); 22 | ``` 23 | 24 | author: 503802922 [scott chen](http://www.classical1988.com/) 25 | 26 | mail: cgyqqcgy@gmail.com 27 | --------------------------------------------------------------------------------