uploadFile
Feature Introduction
Description
Upload a file via the SDK.
- iOS
- Android
- Flutter
- uni-app
- Browser/Electron/MiniProgram
- React-Native
Function Prototype
 Future uploadFile({
    required String id,
    required String filePath,
    required String fileName,
    String? contentType,
    String? cause,
    String? operationID,
  });
Input Parameters
| Parameter Name | Parameter Type | Mandatory | Description | 
|---|---|---|---|
| filePath | String | Yes | Full path of the file on device | 
| id | String | Yes | Unique upload ID | 
| fileName | String | Yes | File name | 
| cause | NSString | No | File category | 
| contentType | NSString | No | File's mimeType | 
Return Value
| Name | Type | Description | 
|---|---|---|
| ~ | String | Returns "{"url":"xxxx"}" | 
Code Example
final result = await OpenIM.iMManager.uploadFile(
        id: const Uuid().v4(),
        filePath: path,
        fileName: path,
      );
      if (result is String) {
            final url = jsonDecode(result)['url'];
            Logger.print('url:$url');
       }
Function Prototype
- (void)uploadFile:(NSString *)fullPath
           name:(NSString * _Nullable)name
          cause:(NSString * _Nullable)cause
     onProgress:(OIMUploadProgressCallback)onProgress
   onCompletion:(OIMUploadCompletionCallback)onCompletion
      onSuccess:(OIMSuccessCallback)onSuccess
      onFailure:(OIMFailureCallback)onFailure;
Input Parameters
| Parameter Name | Parameter Type | Mandatory | Description | 
|---|---|---|---|
| fullPath | NSString | Yes | Full path of the file on device | 
| cause | NSString | No | File category | 
| name | NSString | No | File name | 
Return Value
| Name | Type | Description | 
|---|---|---|
| onProgress | NSInteger | Upload progress | 
| onCompletion | OIMUploadCompletionCallback | Upload completion | 
| onSuccess | OIMSuccessCallback | Successful return | 
| onFailure | OIMFailureCallback | Failed return | 
Code Example
[OIMManager.manager uploadFile:@""
                          name:nil
                         cause:nil
                    onProgress:^(NSInteger saveBytes, NSInteger currentBytes, NSInteger totalBytes) {
} onCompletion:^(NSInteger totalBytes, NSString * _Nonnull url, NSInteger putType) {
} onSuccess:^(NSString * _Nullable data) {
} onFailure:^(NSInteger code, NSString * _Nullable msg) {
}];
Function Prototype
    public void uploadFile(OnBase<String> base, OnPutFileListener listener,
                           PutArgs putArgs)
Input Parameters
PutArgs entity requires the following parameters
| Parameter Name | Parameter Type | Mandatory | Description | 
|---|---|---|---|
| fullPath | String | Yes | Full path of the file on device | 
| putID | String | No | Unique upload ID | 
Code Example
     OpenIMClient.getInstance().uploadFile(new OnBase<String>() {
            
            public void onError(int code, String error) {
            }
            
            public void onSuccess(String data) {
            }
        }, new OnPutFileListener() {
            
            public void hashComplete(String hash, long total) {
            }
            
            public void hashProgress(long current, long total) {
            }
            
            public void open(long size) {
            }
            
            public void putComplete(long total, long putType) {
            }
            
            public void putProgress(long save, long current, long total) {
            }
            
            public void putStart(long current, long total) {
            }
        },putArgs);
Function Prototype
IMSDK.uploadFile({
  name: string;
  contentType: string;
  uuid: string;
  file: File;
},operationID?: string): Promise<WsResponse<{url:string}>>
Input Parameters
| Parameter Name | Parameter Type | Mandatory | Description | 
|---|---|---|---|
| name | string | Yes | File name | 
| contentType | string | Yes | File type | 
| uuid | string | Yes | File's unique ID | 
| file | string | Yes | File's absolute path | 
Return Result
| Parameter Name | Parameter Type | Description | 
|---|---|---|
| Promise.then() | Promise<WsResponse<{url:string}>> | 文件远程链接 | 
| Promise.catch() | Promise<WsResponse> | Callback on failed call | 
Code Example
import { getSDK } from '@openim/wasm-client-sdk';
const IMSDK = getSDK();
// use in electron with ffi
// import { getWithRenderProcess } from '@openim/electron-client-sdk/lib/render';
// const { instance: IMSDK } = getWithRenderProcess();
// use in mini program
// import { getSDK } from '@openim/client-sdk';
// const IMSDK = getSDK();
IMSDK.uploadFile({
  name: 'fileName.zip',
  contentType: 'zip',
  uuid: 'uuid',
  file: File,
})
  .then(({ data: { url } }) => {
    // url: 文件远程链接
  })
  .catch(({ errCode, errMsg }) => {
    // 上传失败
  });
Function Prototype
IMSDK.asyncApi('uploadFile', operationID: string, {
  name: string;
  contentType: string;
  uuid: string;
  filepath: string;
}): Promise<{url:string}>
Input Parameters
| Parameter Name | Parameter Type | Mandatory | Description | 
|---|---|---|---|
| operationID | string | Yes | Operation ID used to pinpoint issues. Unique, recommended using current time and a random number | 
| name | string | Yes | File name | 
| contentType | string | Yes | File type | 
| uuid | string | Yes | File's unique ID | 
| filepath | string | Yes | File's absolute path | 
Return Value
The function is made into a Promise through the
openim-uniapp-polyfillpackage. Usethenandcatchto determine and handle success and failure callbacks.
| Parameter Name | Parameter Type | Description | 
|---|---|---|
| Promise.then() | Promise<{url:string}> | Remote file link | 
| Promise.catch() | Promise<CatchResponse> | Failure callback | 
Code Example
import IMSDK from 'openim-uniapp-polyfill';
IMSDK.asyncApi('uploadFile', IMSDK.uuid(), {
  name: 'fileName.zip',
  contentType: 'zip',
  uuid: 'uuid',
  filepath: 'path://...',
})
  .then((data) => {
    // data: remote file link
  })
  .catch(({ errCode, errMsg }) => {
    // Call failed
  });
Function Prototype
OpenIMSDKRN.uploadFile({
  name: string;
  contentType: string;
  uuid: string;
  filepath: string;
}, operationID: string): Promise<{ url: string }>
Input Parameters
| Parameter Name | Parameter Type | Mandatory | Description | 
|---|---|---|---|
| name | string | Yes | File name | 
| contentType | string | Yes | File type | 
| uuid | string | Yes | File unique ID | 
| filepath | string | Yes | File absolute path | 
| operationID | string | Yes | Operation ID, used for problem location, keep unique, suggest using current time and random number | 
Return Result
| Parameter Name | Parameter Type | Description | 
|---|---|---|
| Promise.then() | Promise<{url:string}> | Remote file link | 
| Promise.catch() | Promise<CatchResponse> | Callback on failed call | 
Code Example
import OpenIMSDKRN from "open-im-sdk-rn";
OpenIMSDKRN.uploadFile({
  name: 'fileName.zip',
  contentType: 'zip',
  uuid: 'uuid',
  filepath: 'path://...',
}, 'operationID')
  .then((data) => {
    // data: remote file link
  })
  .catch(({ errCode, errMsg }) => {
    // Call failed
  });