我在 Angular2 中遇到问题:我有媒体上传模块(图像/视频)。因此,上传模块工作正常,但上传文件后,当我尝试观看视频或查看图像文件时,出现 404 Not Found 错误。但是如果我运行 ng build 并再次刷新它,我可以观看视频或查看图像文件。我做错了什么或者我们可以用某种方式处理这个问题吗?
获取http://localhost:4200/dist/assets/mediauploads/Desktop%20(19).jpg 404(未找到)
//This is my node js code
socket.on('Upload', function (data){
var Name = data['Name'];
Files[Name]['Downloaded'] += data['Data'].length;
Files[Name]['Data'] += data['Data'];
if(Files[Name]['Downloaded'] == Files[Name]['FileSize']) //If File is Fully Uploaded
{
fs.write(Files[Name]['Handler'], Files[Name]['Data'], null, 'Binary', function(err, Writen){
var inp = fs.createReadStream("./src/assets/temp/" + Name);
var out = fs.createWriteStream("./src/assets/mediauploads/" + Name);
inp.pipe(out);
inp.on("end", function() {
var path="./src/assets/temp/" + Name;
var tempFile = fs.openSync(path, 'r');
if(fs.existsSync(path)){
fs.closeSync(tempFile);
fs.unlinkSync(path);
socket.emit('Done', {'Image' : './src/assets/mediauploads/' + Name});
}
});
});
}
else if(Files[Name]['Data'].length > 10485760){ //If the Data Buffer reaches 10MB
fs.write(Files[Name]['Handler'], Files[Name]['Data'], null, 'Binary', function(err, Writen){
Files[Name]['Data'] = ""; //Reset The Buffer
var Place = Files[Name]['Downloaded'] / 524288;
var Percent = (Files[Name]['Downloaded'] / Files[Name]['FileSize']) * 100;
socket.emit('MoreData', { 'Place' : Place, 'Percent' : Percent});
});
}
else
{
var Place = Files[Name]['Downloaded'] / 524288;
var Percent = (Files[Name]['Downloaded'] / Files[Name]['FileSize']) * 100;
socket.emit('MoreData', { 'Place' : Place, 'Percent' : Percent});
}
});
当您运行ng build
时, Angular 应用程序将内置到dist
文件夹中。我的猜测是,您的应用程序设置方式实际上是将图像上传到您的 src/assets
文件夹中。当您从 dist
文件夹运行应用程序时,您新上传的图像/视频不可见。当您重建应用程序时,文件将被复制到 dist
文件夹并可供访问。
我是一名优秀的程序员,十分优秀!