docs: 上传和下载使用介绍
parent
61337a9bbe
commit
f1c0f348be
|
@ -32,6 +32,8 @@ export default defineConfig({
|
|||
{ text: 'transformRequest', link: '/config/transform-request' },
|
||||
{ text: 'transformResponse', link: '/config/transform-response' },
|
||||
{ text: 'errorHandler', link: '/config/error-handler' },
|
||||
{ text: 'upload', link: '/config/upload' },
|
||||
{ text: 'download', link: '/config/download' },
|
||||
{ text: 'adapter', link: '/config/adapter' },
|
||||
],
|
||||
},
|
||||
|
@ -41,6 +43,7 @@ export default defineConfig({
|
|||
{ text: 'interceptors', link: '/api/interceptors' },
|
||||
{ text: 'CancelToken', link: '/api/cancel-token' },
|
||||
{ text: 'isCancel', link: '/api/is-cancel' },
|
||||
{ text: 'isAxiosError', link: '/api/is-axios-error' },
|
||||
{ text: 'getUri', link: '/api/get-uri' },
|
||||
{ text: 'create', link: '/api/create' },
|
||||
{ text: 'Axios', link: '/api/axios' },
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
# isAxiosError
|
||||
|
||||
## `axios.isAxiosError` 是否是 AxiosError
|
||||
|
||||
可以判断当前错误是否来自请求响应,而不是语法错误或者用户主动抛出的错误
|
||||
|
||||
```typescript
|
||||
axios('/user').catch((error) => {
|
||||
if (axios.isAxiosError(error)) {
|
||||
// 错误是否来自原生接口的 fail 回调
|
||||
// 如果错误来自对状态码的判断,这种情况是没有这个属性的
|
||||
error.isFail;
|
||||
// 请求配置
|
||||
error.config;
|
||||
// 请求任务
|
||||
error.request;
|
||||
// 响应体
|
||||
error.response;
|
||||
}
|
||||
});
|
||||
```
|
|
@ -0,0 +1,27 @@
|
|||
# download
|
||||
|
||||
## 发送下载请求
|
||||
|
||||
可以从服务端下载文件本地,只有 get 请求才生效
|
||||
|
||||
```typescript
|
||||
axios
|
||||
.get(
|
||||
'/file',
|
||||
{
|
||||
// 指定文件下载后存储的路径 (本地路径),选填
|
||||
filePath: '',
|
||||
// 指定文件下载后存储的名称,选填
|
||||
fileName: '',
|
||||
},
|
||||
{
|
||||
download: true,
|
||||
},
|
||||
)
|
||||
.then((response) => {
|
||||
// 用户文件路径 (本地路径)。传入 filePath 时会返回,跟传入的 filePath 一致
|
||||
response.data.filePath;
|
||||
// 用户文件路径 (本地临时路径)。
|
||||
response.data.tempFilePath;
|
||||
});
|
||||
```
|
|
@ -0,0 +1,25 @@
|
|||
# upload
|
||||
|
||||
## 发送上传请求
|
||||
|
||||
可以上传文件到服务端,只有 post 请求才生效
|
||||
|
||||
```typescript
|
||||
axios.post(
|
||||
'/file',
|
||||
{
|
||||
// 文件名称,必填
|
||||
fileName: 'image.png',
|
||||
// 文件路径,必填
|
||||
filePath: '/file/image.png',
|
||||
// 文件类型,选填
|
||||
fileType: 'image' | 'video' | 'audio';
|
||||
// 可以传入更多自定义字段,这些自定义字段最终会以 formData 的形式发送给服务端 (前提是平台支持)
|
||||
custom1: 'name',
|
||||
custom2: 'id'
|
||||
},
|
||||
{
|
||||
upload: true,
|
||||
},
|
||||
);
|
||||
```
|
|
@ -274,22 +274,17 @@ export function createAdapter(platform: AxiosPlatform): AxiosAdapter {
|
|||
}
|
||||
|
||||
function injectDownloadData(response: AnyObject): void {
|
||||
response.data = response.data || {};
|
||||
response.data = {
|
||||
filePath: response.filePath,
|
||||
tempFilePath:
|
||||
response.tempFilePath ||
|
||||
// response.apFilePath 为支付宝小程序基础库小于 2.7.23 的特有属性。
|
||||
response.apFilePath,
|
||||
};
|
||||
|
||||
if (response.tempFilePath) {
|
||||
response.data.tempFilePath = response.tempFilePath;
|
||||
delete response.tempFilePath;
|
||||
}
|
||||
|
||||
if (response.apFilePath) {
|
||||
response.data.tempFilePath = response.apFilePath;
|
||||
delete response.apFilePath;
|
||||
}
|
||||
|
||||
if (response.filePath) {
|
||||
response.data.filePath = response.filePath;
|
||||
delete response.filePath;
|
||||
}
|
||||
if (response.tempFilePath) delete response.tempFilePath;
|
||||
if (response.apFilePath) delete response.apFilePath;
|
||||
if (response.filePath) delete response.filePath;
|
||||
}
|
||||
|
||||
return adapter;
|
||||
|
|
Loading…
Reference in New Issue