uploadFile
Description
info
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 | Required | Description |
|---|---|---|---|
| filePath | String | Yes | Absolute path of the file |
| id | String | Yes | Unique ID for the upload |
| fileName | String | Yes | File name |
| cause | NSString | No | File category |
| contentType | NSString | No | mimeType of the file |
Return Result
| 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 | Required | Description |
|---|---|---|---|
| fullPath | NSString | Yes | Absolute path of the file |
| cause | NSString | No | File category |
| name | NSString | No | File name |
Return Result
| Name | Type | Description |
|---|---|---|
| onProgress | NSInteger | Upload progress |
| onCompletion | OIMUploadCompletionCallback | Upload completed |
| onSuccess | OIMSuccessCallback | Success return |
| onFailure | OIMFailureCallback | Failure 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
The PutArgs entity must pass the following two parameters:
| Parameter Name | Parameter Type | Required | Description |
|---|---|---|---|
| fullPath | String | Yes | Absolute path of the file |
| putID | String | No | Unique ID for the upload |
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;
filepath?: string; // Only effective when called using ffi in electron
cause?: string
},operationID?: string): Promise<WsResponse<{url:string}>>
Input Parameters
| Parameter Name | Parameter Type | Required | Description |
|---|---|---|---|
| name | string | Yes | File name |
| contentType | string | Yes | File type |
| uuid | string | Yes | Unique ID of the file |
| file | string | No | File object |
| filepath | string | No | Absolute path of the file, only effective when called using ffi in electron |
| cause | string | No | File category |
Return Result
| Parameter Name | Parameter Type | Description |
|---|---|---|
| Promise.then() | Promise<WsResponse<{url:string}>> | Remote link of the file |
| Promise.catch() | Promise<WsResponse> | Failure callback |
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',
cause: '',
file: File, // Pass file object on web
// filepath: 'path://...' // Only effective when called using ffi in electron, 'file' is not needed then
})
.then(({ data: { url } }) => {
// url: Remote link of the file
})
.catch(({ errCode, errMsg }) => {
// Upload failed
});
Function Prototype
IMSDK.asyncApi('uploadFile', operationID: string, {
name: string;
contentType: string;
uuid: string;
filepath: string;
}): Promise<{url:string}>
Input Parameters
| Parameter Name | Parameter Type | Required | Description |
|---|---|---|---|
| operationID | string | Yes | Operation ID, used to pinpoint issues, keep unique, prefer time+random |
| name | string | Yes | File name |
| contentType | string | Yes | File type |
| uuid | string | Yes | Unique ID of the file |
| filepath | string | Yes | Absolute path of the file |
Return Result
Use the
openim-uniapp-polyfillpackage to make the function Promise. When calling, you need to usethenandcatchto determine and handle success and failure callbacks.
| Parameter Name | Parameter Type | Description |
|---|---|---|
| Promise.then() | Promise<{url:string}> | Remote link of the file |
| 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 link of the file
})
.catch(({ errCode, errMsg }) => {
// Call failed
});
Function Prototype
OpenIMSDK.uploadFile({
name: string;
contentType: string;
uuid: string;
filepath: string;
}, operationID?: string): Promise<{ url: string }>
Input Parameters
| Parameter Name | Parameter Type | Required | Description |
|---|---|---|---|
| name | string | Yes | File name |
| contentType | string | Yes | File type |
| uuid | string | Yes | Unique ID of the file |
| filepath | string | Yes | Absolute path of the file |
| operationID | string | No | Operation ID, used to pinpoint issues, keep unique, prefer time+random |
Return Result
| Parameter Name | Parameter Type | Description |
|---|---|---|
| Promise.then() | Promise<{url:string}> | Remote link of the file |
| Promise.catch() | Promise<OpenIMApiError> | Failure callback |
Code Example
import OpenIMSDK from "@openim/rn-client-sdk";
OpenIMSDK.uploadFile({
name: 'fileName.zip',
contentType: 'zip',
uuid: 'uuid',
filepath: 'path://...',
})
.then((data) => {
// data: Remote link of the file
})
.catch((error) => {
// Call failed
});