video.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { Component } from 'react';
  2. import { Upload, message, Icon, Modal,Spin } from 'antd';
  3. class VideoComponent extends Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. previewVisible: false,
  8. previewImage: '',
  9. loading:false,
  10. fileList: [],
  11. fileListUrl:''
  12. };
  13. }
  14. handleCancel() {
  15. this.setState({ previewVisible: false });
  16. }
  17. handlePreview(file) {
  18. this.setState({
  19. previewImage: file.url ,
  20. previewVisible: true
  21. });
  22. }
  23. handleChange(info) {
  24. this.props.loadState(true);
  25. this.setState({loading:true})
  26. let fileList = info.fileList;
  27. this.setState({ fileList });
  28. if (info.file.status !== 'uploading') {
  29. }
  30. if (info.file.status === 'done') {
  31. message.success(`上传成功`);
  32. this.props.fileList(fileList);
  33. this.props.loadState(false);
  34. this.setState({
  35. loading:false,
  36. fileListUrl:fileList[0].response.data,
  37. })
  38. } else if (info.file.status === 'error') {
  39. message.error(`上传失败`);
  40. this.props.loadState(false);
  41. }
  42. }
  43. beforeUpload(info){
  44. if(this.props.dataUrl.type.indexOf('video')>-1){
  45. if(info.type.indexOf('video')<0){
  46. message.warning('请上传MP4格式视频文件.');
  47. return false;
  48. };
  49. let sizeMb = info.size/(1024*1024),size=100;
  50. if(sizeMb>size){ //100Mb之内
  51. message.warning(`视频大小不超过${size}`);
  52. return false;
  53. };
  54. };
  55. if(this.props.dataUrl.type.indexOf('image')>-1){
  56. if(info.type.indexOf('image')<0){
  57. message.warning('请上传图片.');
  58. return false;
  59. };
  60. let sizeMb = info.size/(1024*1024),size=1;
  61. if(sizeMb>size){ //1Mb之内
  62. message.warning(`图片大小不超过${size}MB.`);
  63. return false;
  64. };
  65. }
  66. }
  67. componentWillReceiveProps(nextProps) {
  68. this.state.fileList = nextProps.videoUrl;
  69. }
  70. render() {
  71. const { previewVisible, previewImage, fileList } = this.state;
  72. const uploadButton = (
  73. <div>
  74. <Icon type="plus" />
  75. <div className="ant-upload-text">点击上传</div>
  76. </div>
  77. );
  78. const dataUrl = this.props.dataUrl||{};
  79. if(this.state.loading){
  80. return false;
  81. }
  82. return (
  83. <div className="clearfix">
  84. <Spin spinning={this.state.loading}>
  85. <Upload
  86. action={globalConfig.context + dataUrl.url}
  87. data={{ sign:dataUrl.keyWord }}
  88. listType="picture-card"
  89. fileList={fileList}
  90. beforeUpload={this.beforeUpload.bind(this)}
  91. onPreview={this.handlePreview.bind(this)}
  92. onChange={this.handleChange.bind(this)}
  93. >
  94. {fileList.length >= this.props.dataUrl.page ? null : uploadButton}
  95. </Upload>
  96. {previewImage&&<Modal maskClosable={false} visible={previewVisible} footer={null} onCancel={this.handleCancel.bind(this)}>
  97. {this.props.dataUrl.type.indexOf('video')>-1?<video alt="example" style={{ width: '100%' }} src={previewImage||'http://118.190.205.7:8000/'+this.state.fileListUrl} controls></video>
  98. :<img alt="example" style={{ width: '100%' }} src={previewImage} />}
  99. </Modal>}
  100. </Spin>
  101. </div>
  102. );
  103. }
  104. }
  105. export default VideoComponent;