将大文件上传到oneDrive iOS SDk的一个驱动器?

时间:2017-10-17 04:25:25

标签: swift sdk onedrive

我坚持工作,因为我只能将一个小kb大小的文件上传到一个驱动器,这是不够的。如何使用Sdk将大文件上传到一个驱动器。现在我正在使用这个示例项目。 https://github.com/microsoftgraph/ios-swift-snippets-sample

此处上传仅针对图像和小尺寸kb文件显示到onedrive。我们如何使用Session上传大文件?

 func uploadFileToOneDrive(fileName : String,fileId : String, filePath : String){
    print("filePath \(filePath)")
    do {
        let fileData = try! Data(contentsOf: NSURL(fileURLWithPath: filePath) as URL)
        let fileLength = UInt64(fileData.count)
        print(fileLength)
        graphClient.me().drive().items(fileId + ":/createUploadSession").contentRequest().upload(from: fileData, completion: {
            (updatedItem, error) in
            if let nsError = error {
                self.delegate?.errorHandling(error: nsError.localizedDescription)
            }
            else {
                self.delegate?.Success(Sucess: NSLocalizedString("Uploaded", comment: ""))
                self.oneDriveListFiles(path: fileId, folderId: fileId, isBack: true)
            }

            print(self.progress.fractionCompleted)
        })
    }
}

还帮我处理progressBar上传和下载?

1 个答案:

答案 0 :(得分:0)

以下是图片上传和展示下载进度的解决方案。这里将使用AFNetworking库:

- (void)uploadImageToServer{

    NSData *imageData = UIImageJPEGRepresentation(_uploadImage, 0.5);

    NSString *urlString = @"<YOUR URL>;

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

 manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [manager POST:urlString parameters:_param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

        [formData appendPartWithFileData:imageData name:@"uploaded_file" fileName:@"uploaded_file.jpg" mimeType:@"image/jpeg"];

    } progress:^(NSProgress * _Nonnull uploadProgress) {
        [manager setTaskDidSendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
            float processbar = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
            dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
                //do something
                dispatch_async(dispatch_get_main_queue(), ^{
                    _progressView.progress = processbar;
                    _lblProgressPercentage.text = [NSString stringWithFormat:@"%.0f%%",(processbar*100)];

                });
                //do something else
            });
        }];

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {


    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Error: %@", error);

    }];
}