简体   繁体   English

上传到angular 5 FE时,HTML5视频显示为白色

[英]HTML5 video showing up as white when uploaded to angular 5 FE

Have a weird issue right now, I can upload a file to local storage, upload its path to mongodb. 现在有一个奇怪的问题,我可以将文件上传到本地存储,将其路径上传到mongodb。 Everything working fine and ready to display in the angular front end. 一切正常,并准备在倾斜的前端显示。 For some reason unless I manually close down the code and reopen it the video only shows up as white, even though the path is absolutely correct. 由于某种原因,除非我手动关闭代码并重新打开,否则即使路径绝对正确,视频也只会显示为白色。 Refreshing the page does nothing, video still white. 刷新页面无济于事,视频仍为白色。 But restarting the application then the video shows. 但是重新启动应用程序,然后视频显示。 Is anyone able to help me? 有人可以帮助我吗? I can not figure out whats going wrong, so I can't learn, would really apreciate some assistance with this! 我不知道出了什么问题,所以我无法学习,真的会为此提供一些帮助!


 import { Component, OnInit, OnChanges } from '@angular/core'; import { HttpClient } from '@angular/common/http' @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { title = 'app'; videos = new Array; constructor(private http: HttpClient) { } ngOnInit() { this.getVideos() } getVideos() { this.http.get<any>('/getVideos') .subscribe((data) => { data.forEach(element => { this.videos.push(element) }); }) } } 
 <form action="/upload" method="POST" enctype="multipart/form-data"> <div class="file-field input-field"> <div class="btn grey"> <span>File</span> <input name="myImage" type="file"> </div> <div class="file-path-wrapper"> <input class="file-path validate" type="text"> </div> </div> <button type="submit" (click)="id()" class="btn">Submit</button> </form> <br> <br> <div *ngFor="let video of videos"> <video width="300" controls> <source [src]="video.fileURL" type="video/mp4"> <source [src]="video.fileURL" type="video/ogg"> Your browser does not support HTML5 video. </video><br><br> </div> 

 const express = require('express'); const path = require('path'); const fileUpload = require('express-fileupload'); const mongoose = require('mongoose'); const app = express(); require('./models/Upload'); const Upload = mongoose.model('upload'); // default options app.use(fileUpload()); // set static folder app.use(express.static(__dirname + '/dist')); // Mongoose/MongoDB middleware const db = require('./config/database'); mongoose.Promise = global.Promise; mongoose.connect(db.mongoURI) .then(() => console.log('MongoDB Connected...')) .catch(err => console.log(err)); app.get('/getVideos', (req, res) => { Upload.find() .then((data) => { res.json(data) }) }) app.post('/upload', (req, res) => { if (!req.files) console.log('no files selected') // The name of the input field (ie "sampleFile") is used to retrieve the uploaded file let sampleFile = req.files.myImage; let name = req.files.myImage.name + Date.now(); fileLocation = `../assets/uploads/${name}.mp4`; // Use the mv() method to place the file somewhere on your server sampleFile.mv(`./src/assets/uploads/${name}.mp4`, function (err) { if (err) { console.log('Moving file error ' + err) } else { const newUpload = { fileURL: fileLocation, fileName: name, userID: '0001' } new Upload(newUpload) .save() console.log('file uploaded') res.redirect('back'); } }); }); //catch all routes and send to Angular Router. app.get('*/', (req, res) => { res.sendFile(path.join(__dirname + '/dist/index.html')); }) // Set listening port const port = process.env.PORT || 1337 app.listen(port, () => { console.log(`Server running on ${port}`) }) 

See below console log from google chrome, showing that the path is correct. 请参阅下面来自Google Chrome的控制台日志,显示路径正确。 But the video will not show? 但是视频不会显示吗?

 <div _ngcontent-c0=""> <video _ngcontent-c0="" controls="" width="300"> <source _ngcontent-c0="" type="video/mp4" src="../assets/uploads/Bye fellows.mp41521933023054.mp4"> <source _ngcontent-c0="" type="video/ogg" src="../assets/uploads/Bye fellows.mp41521933023054.mp4"> Your browser does not support HTML5 video. </video><br _ngcontent-c0=""><br _ngcontent-c0=""> </div> <div _ngcontent-c0=""> <video _ngcontent-c0="" controls="" width="300"> <source _ngcontent-c0="" type="video/mp4" src="../assets/uploads/Hello there!.mp41521933225754.mp4"> <source _ngcontent-c0="" type="video/ogg" src="../assets/uploads/Hello there!.mp41521933225754.mp4"> Your browser does not support HTML5 video. </video><br _ngcontent-c0=""><br _ngcontent-c0=""> </div> 

Would really appreciate some help as to why it wont show a video up unless I manually restart the code. 非常感谢您提供一些帮助,除非我手动重新启动代码,否则为什么它不会显示视频。 The path is right, everything seems to be working normal. 道路是正确的,一切似乎都正常。 Just angular wont display it for some reason. 由于某些原因,只是角度不会显示它。

Thanks all! 谢谢大家!

Your angular project runs asynchronously. 您的角度项目异步运行。 For that reason, you are getting a blank page when the videos have not been retrieved from the database. 因此,当尚未从数据库中检索视频时,您将获得空白页。 In order to fix this issue, you should use *ngIf . 为了解决此问题,您应该使用*ngIf Below is a change to your current code. 以下是对您当前代码的更改。

<div *ngIf="videos.length > 0">
 <div *ngFor="let video of videos">

  <video width="300" controls>
      <source [src]="video.fileURL" type="video/mp4">
      <source [src]="video.fileURL" type="video/ogg">
      Your browser does not support HTML5 video.
    </video><br><br>

 </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM