aliyun-oss-react-native

RNAliyunOSS+UPLOAD.m 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // RNAliyunOSS+UPLOAD.m
  3. // aliyun-oss-rn-sdk
  4. // Created by 罗章 on 2018/5/8.
  5. #import "RNAliyunOSS+UPLOAD.h"
  6. @implementation RNAliyunOSS (UPLOAD)
  7. /**
  8. Asynchronous uploading
  9. */
  10. RCT_REMAP_METHOD(asyncUpload, asyncUploadWithBucketName:(NSString *)bucketName objectKey:(NSString *)objectKey filepath:(NSString *)filepath options:(NSDictionary*)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
  11. [self beginUploadingWithFilepath:filepath resultBlock:^(NSData *data) {
  12. OSSPutObjectRequest *put = [OSSPutObjectRequest new];
  13. //required fields
  14. put.bucketName = bucketName;
  15. put.objectKey = objectKey;
  16. put.uploadingData = data;
  17. // 设置Content-Type,可选
  18. // put.contentType = @"application/octet-stream";
  19. // // 设置MD5校验,可选
  20. // put.contentMd5 = [OSSUtil base64Md5ForFilePath:@"<filePath>"]; // 如果是文件路径
  21. //optional fields
  22. put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
  23. NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
  24. // Only send events if anyone is listening
  25. if (self.hasListeners) {
  26. [self sendEventWithName:@"uploadProgress" body:@{@"bytesSent":[NSString stringWithFormat:@"%lld",bytesSent],
  27. @"currentSize": [NSString stringWithFormat:@"%lld",totalByteSent],
  28. @"totalSize": [NSString stringWithFormat:@"%lld",totalBytesExpectedToSend]}];
  29. }
  30. };
  31. OSSTask *putTask = [self.client putObject:put];
  32. [putTask continueWithBlock:^id(OSSTask *task) {
  33. if (!task.error) {
  34. NSLog(@"upload object success!");
  35. resolve(task.description);
  36. } else {
  37. NSLog(@"upload object failed, error: %@" , task.error);
  38. reject(@"Error", @"Upload failed", task.error);
  39. }
  40. return nil;
  41. }];
  42. }];
  43. }
  44. @end