Last update:2020-09-28 16:57:58
MobileCoreServices.framework
libz.dylib(libz.tbd for Xcode7+)
For server end development environment please refer to wcs-Java-SDK
self.client = [[WCSClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://yourUploadDomain.com"] andTimeout:30];
- (void)normalUpload {
WCSUploadObjectRequest *request = [[WCSUploadObjectRequest alloc] init];
request.token = @"token of uploading, provided by server end";
request.fileName = @"File name";
request.key = @"the file name in cloud storage, if remain it empty, it will follow as fileName";
request.fileData = fileData; // The file need to be uploaded
request.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld bytes sent, %lld total bytes sent, %lld total byte exptected", bytesSent, totalBytesSent, totalBytesExpectedToSend);
};
WCSClient *client = [[WCSClient alloc] initWithBaseURL:nil andTimeout:30];
// Notes: If you are using callback upload, you need to use uploadRequestRaw, which will avoid unnecessary abnormity in base64 resolve
[[client uploadRequest:request] continueWithBlock:^id _Nullable(WCSTask<WCSUploadObjectResult *> * _Nonnull task) {
if (task.error) {
NSLog(@"The request failed. error: [%@]", task.error);
} else {
// Request successfully, the content returned by server are as belows
NSDictionary *responseResult = task.result.results;
}
return nil;
}];
}
Example
- (void)normalUploadCancelled {
WCSUploadObjectRequest *request = [[WCSUploadObjectRequest alloc] init];
request.token = @"Token for upload, provided by server end";
request.fileName = @"File name";
request.key = @"the file name in cloud storage, if remain it empty, it will follow as fileName";
request.fileData = fileData; // The file need to be uploaded
request.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld bytes sent, %lld total bytes sent, %lld total byte exptected", bytesSent, totalBytesSent, totalBytesExpectedToSend);
};
WCSClient *client = [[WCSClient alloc] initWithBaseURL:nil andTimeout:30];
[[client uploadRequest:request] continueWithBlock:^id _Nullable(WCSTask<WCSUploadObjectResult *> * _Nonnull task) {
if (task.error) {
// Request is cancelled
if (task.error.code == NSURLErrorCancelled) {
NSLog(@"request cancelled.");
} else {
NSLog(@"%@", task.error);
}
} else {
NSLog(@"%@", task.result.results);
}
return nil;
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[uploadRequest cancel];
});
}
Examples
- (void)normalUpload {
WCSUploadObjectRequest *request = [[WCSUploadObjectRequest alloc] init];
request.token = @"token of uploading, provided by server end";
// Customized variables, which will be returned to client end after successful upload
request.customParams = @{@"x:test" : @"customParams"};
request.fileName = @"File name";
request.key = @"the file name in cloud storage, if remain it empty, it will follow as fileName";
request.fileData = fileData; // The file need to be uploaded
request.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld bytes sent, %lld total bytes sent, %lld total byte exptected", bytesSent, totalBytesSent, totalBytesExpectedToSend);
};
// it is recommended to use WCSClient
WCSClient *client = [[WCSClient alloc] initWithBaseURL:nil andTimeout:30];
// Notes: If you are using callback upload, you need to use uploadRequestRaw, which will avoid unnecessary abnormity in base64 resolve
[[client uploadRequest:request] continueWithBlock:^id _Nullable(WCSTask<WCSUploadObjectResult *> * _Nonnull task) {
if (task.error) {
NSLog(@"The request failed. error: [%@]", task.error);
} else {
// Request successfully, the content returned by server are as belows
NSDictionary *responseResult = task.result.results;
}
return nil;
}];
}
Normally, it takes a long time to upload large files on the mobile end. Once abnormalities occur in the transmission process, all files need to be retransmitted, which will affect the user experience. To avoid this problem, multipart upload mechanism is introduced.
Multipart upload slice a large file into many custom sized blocks, and then upload these blocks in parallel. Once a block upload fails, the client just needs to re-upload the block.
Note: The maximum size of each block should not exceed 100M and the minimum size should not be less than 4M.
Example
- (void)chunkedUpload {
WCSBlockUploadRequest *blockRequest = [[WCSBlockUploadRequest alloc] init];
blockRequest.fileKey = @"the file name in cloud storage, if remain it empty, it will follow as fileName";
blockRequest.fileURL = fileURL; // URL of this file
blockRequest.token = @"token of uploading, provided by server end";
blockRequest.chunkSize = 256 * 1024; // Note: the chunk size must be multiple of 64K, and the max size can't exceed the block size
blockRequest.blockSize = 4 * 1024 * 1024; // Note: the bock size must be multiple of 4M, and the max size can't exceed 100M
[blockRequest setUploadProgress:^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%@ %@", @(totalBytesSent), @(totalBytesExpectedToSend));
}];
// it is recommended to use WCSClient
WCSClient *client = [[WCSClient alloc] initWithBaseURL:nil andTimeout:30];
// Notes: If you are using callback upload, you need to use blockUploadRequestRaw, which will avoid unnecessary abnormity in base64 resolve
[[client blockUploadRequest:blockRequest] continueWithBlock:^id _Nullable(WCSTask<WCSBlockUploadResult *> * _Nonnull task) {
if (task.error) {
NSLog(@"error %@", task.error.localizedDescription);
} else {
// Request successfully, the content returned by server are as belows
NSLog(@"results %@", task.result.results);
}
return nil;
}];