问题
I am working on project in ReactJS, Currently I am using ant-design for file uploading. I want to preview video when user select a video . I am new to reactjs and unable to complete my task. Could someone please help me how it would be possible to preview a video . I will share my code with you You may check my code . Thanks
Code
import React from 'react';
import styled from 'styled-components';
import 'antd/dist/antd.css';
import './styletwo.css';
import { Form, Upload, Button, Icon } from 'antd';
// import reqwest from 'reqwest';
const Dragger = Upload.Dragger;
const FormItem = Form.Item;
const PhotoText = styled.div`
font-size: 16px;
font-weight: bold;
padding: 2rem 0 1rem 0;
display: block;
text-align: -webkit-auto;
`;
const props = {
action: '',
listType: 'picture',
};
class RegisterStepTwo extends React.Component {
render() {
const { getFieldDecorator } = this.props;
return (
<div>
<PhotoText>Select a Photo to Upload</PhotoText>
<FormItem>
{getFieldDecorator('picture', {
rules: [
{
required: true,
message: 'Please upload picture!',
},
],
})(
<Dragger {...props}>
<p className="ant-upload-drag-icon">
<Icon type="upload" />
</p>
<p className="ant-upload-text">
Click or drag photo to this area to upload
</p>
</Dragger>,
)}
</FormItem>
<PhotoText>Select a Video to Upload</PhotoText>
<FormItem>
{getFieldDecorator('video', {
rules: [
{
required: true,
message: 'Please upload video!',
},
],
})(
<Dragger>
<p className="ant-upload-drag-icon">
<Icon type="upload" />
</p>
<p className="ant-upload-text">
Click or drag Video to this area to upload
</p>
</Dragger>,
)}
</FormItem>
</div>
);
}
}
export default RegisterStepTwo;
回答1:
First read blob data from your file
let reader = new FileReader();
//if reading completed
reader.onload = e => {
let blobData = e.target.result); //blob data
console.log(blobData);
};
//read the file
reader.readAsDataURL(file);
Once read as blob you can use html5 video tag to show the video like following
<video width="400" controls>
<source src={blobData} type={fileType}/>
Your browser does not support HTML5 video.
</video>
Please have a look to my crude CodeSandBox https://codesandbox.io/s/wq2jnm9wpk
I hope this would help you.
来源:https://stackoverflow.com/questions/53631198/how-to-preview-a-video-in-reactjs