关于antd上传视频无法预览提供一种思路
使用upload上传视频时,预览按钮是灰色的无法点击,并且没有缩略图,我这边的解决方法是,自定义一个预览,点击预览时,直接跳转到另一个页面,缩率图可以使用一张网图从网上或者是直接存储在服务器上的。
handlePreviewFile(file) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
const dataUrl = e.target.result;
resolve(“https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3498227956,2363956367&fm=26&gp=0.jpg”)
}
})
}
在upload中previewFile={this.handlePreviewFile},此时视频的预览的小眼睛就可以点击了,resolve中的是我上传的一张网图,也就是用于替换视频的缩略图。
在state中定义previewVideoUrl
在beforeupload中获取视频的地址
const url = URL.createObjectURL(file)
this.setState(state => ({
previewVideoUrl:url,
}))
handlePreviewVideo =()=> {
const files=this.state.previewVideoUrl
window.open(files)
};
最后在upload中 onPreview={this.handlePreviewVideo}
此时就可以看到缩略图了,也能点击预览跳转页面播放视频。
来自刚学react的萌新,有用请点个赞
关于antd上传视频无法预览提供一种思路使用upload上传视频时,预览按钮是灰色的无法点击,并且没有缩略图,我这边的解决方法是,自定义一个预览,点击预览时,直接跳转到另一个页面,缩率图可以使用一张网图从网上或者是直接存储在服务器上的。handlePreviewFile(file) {return new Promise((resolve) => {const reader = ne...
前端页面代码部分
<template>
<a-
upload
list-type="picture-card" :customRequest="imgAdd" :remove="imgDel"
:
file
-list="
file
List" @
preview
="handle
Preview
" @change="handleChange">
<div v-if="
file
List.length < 9">
当成功时返回
上传
到oss的路径,以及文件的类型
首先需要一个
上传
文件的组件a-
upload
,一个进度条, 在before
Upload
中对文件进行文件类型的检测,在customRequest
上传
文件到阿里云 oss
## 1.before
Upload
对文件类型进
可以使用src/
antd
-theme-override.less来编辑
antd
默认变量
编辑CSS
Antd
类
您还可以编辑css文件src/style-override.css以覆盖/编辑
antd
类的样式
完成变量和样式的编辑后,通过此命令导出
yarn export
导出的文件将在dist目录中找到,并在您的项目中复制和使用
故事已基于
antd
存储库创建。 您还可以通过git子模块导入
antd
组件并运行脚本来创建或更新故事来更新它们。
git submodule update --init --recursive // Adds
antd
subm
要开始运行以下命令:
# clone this repository
git clone https://github.com/davidsonsns/storybook-
antd
.git
# enter in the folder
cd storybook-
antd
# install the dependencies
# run the project
yarn storybook
在浏览器中打开URL 。
import React, { memo, useState } from 'react'
import {
Upload
} from '
antd
'
import ImgCrop from '
export default memo(
1<styletype="text/css">#new
Preview
{
2FILTER:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)
4</style>
6head内
10bo...
<a-
upload
:
file
-list="
file
List" action="#" accept=".png" :custom- request="handle
Upload
3" :show
Upload
List="false">
<a-button> <a-icon type="
upload
"></a-icon>
上传
</a-button>
</a-
upload
>
Ant Design Mobile(
antd
-mobile)并没有
提供
上传
视频
的组件,但是可以使用 Ant Design 的 `
Upload
` 组件来实现
上传
视频
的功能。具体步骤如下:
1. 安装 Ant Design 和 Ant Design Mobile:
```bash
npm install
antd
antd
-mobile --save
2. 在需要使用
上传
组件的页面引入 `
Upload
` 组件和 `Button` 组件:
```jsx
import {
Upload
, Button } from '
antd
';
3. 在页面中添加 `
Upload
` 组件:
```jsx
<
Upload
>
<Button>
上传
视频
</Button>
</
Upload
>
4. 在 `
Upload
` 组件中添加
上传
视频
的逻辑。可以使用 `before
Upload
` 方法来验证
上传
的文件格式和大小,并使用 `customRequest` 方法将
视频
上传
到服务器。
```jsx
<
Upload
before
Upload
={this.before
Upload
}
customRequest={this.
upload
Video}
<Button>
上传
视频
</Button>
</
Upload
>
其中,`before
Upload
` 方法的实现可以参考下面的代码:
```jsx
// 验证
上传
的文件格式和大小
before
Upload
= (
file
) => {
const isVideo =
file
.type === 'video/mp4';
if (!isVideo) {
message.error('只能
上传
MP4 格式的
视频
!');
const isLt100M =
file
.size / 1024 / 1024 < 100;
if (!isLt100M) {
message.error('
视频
大小不能超过 100MB!');
return isVideo && isLt100M;
`
upload
Video` 方法的实现可以参考下面的代码:
```jsx
// 将
视频
上传
到服务器
upload
Video = (options) => {
const {
file
, onSuccess, onError } = options;
const formData = new FormData();
formData.append('video',
file
);
// 发送 POST 请求
上传
视频
axios.post('/api/
upload
Video', formData).then((response) => {
onSuccess(response.data,
file
);
}).catch((error) => {
onError(error);
完整代码实现如下:
```jsx
import React from 'react';
import {
Upload
, Button, message } from '
antd
';
import axios from 'axios';
class
Upload
Video extends React.Component {
// 验证
上传
的文件格式和大小
before
Upload
= (
file
) => {
const isVideo =
file
.type === 'video/mp4';
if (!isVideo) {
message.error('只能
上传
MP4 格式的
视频
!');
const isLt100M =
file
.size / 1024 / 1024 < 100;
if (!isLt100M) {
message.error('
视频
大小不能超过 100MB!');
return isVideo && isLt100M;
// 将
视频
上传
到服务器
upload
Video = (options) => {
const {
file
, onSuccess, onError } = options;
const formData = new FormData();
formData.append('video',
file
);
// 发送 POST 请求
上传
视频
axios.post('/api/
upload
Video', formData).then((response) => {
onSuccess(response.data,
file
);
}).catch((error) => {
onError(error);
render() {
return (
<
Upload
before
Upload
={this.before
Upload
}
customRequest={this.
upload
Video}
<Button>
上传
视频
</Button>
</
Upload
>
export default
Upload
Video;
需要注意的是,
上传
视频
的具体实现要根据后端 API 的要求来进行调整。以上代码仅供参考。