我正在尝试允许用户使用常规javascript中的输入和FileReader()将歌曲下载到音频播放列表中,但没有显示任何内容。我已经用普通的Javascript完成了我的大部分代码,我觉得很难把它转换成所有的ReactJS代码。
import React from 'react';
import {
Link
} from 'react-router-dom';
import $ from "jquery";
var Download = (props) => {
function other() {
console.log("It's working");
var input = document.createElement("INPUT");
input.id = "upload-file";
input.type = "file";
input.multiple = true;
var cloneInput = input.cloneNode(true);
cloneInput.id = "cloneInput";
var destinationDiv = document.createElement("div");
destinationDiv.id = "destination";
var destination = document.getElementById('destination');
destinationDiv.innerHTML = "destination";
var appendInput = document.body.appendChild(input);
if (appendInput) {
console.log("yay");
}
document.body.appendChild(destinationDiv);
var inputId = document.getElementById('upload-file');
if (inputId) {
inputId.addEventListener('change', function() {
var file;
var destination = document.getElementById('destination');
var audioAlreadyUsed = true;
var ulAlreadyAdded = true;
var alreadyUsed = true;
// Looping in case they uploaded multiple files
for (var x = 0, xlen = this.files.length; x < xlen; x++) {
let a = document.createElement('a');
file = this.files[x];
a.innerHTML = file.name;
console.log(file.name);
if (file.type.indexOf('audio/mp3') != -1) { // Very primitive "validation"
var reader = new FileReader();
reader.onload = function(e) {
var ul = document.createElement("UL");
ul.id = "playlist";
var audio = document.createElement("AUDIO");
audio.id = "audio";
// audio.src = e.target.result;
audio.controls = true;
audio.preload = "none";
if (audioAlreadyUsed) {
destination.appendChild(audio);
audioAlreadyUsed = false;
}
if (ulAlreadyAdded) {
destination.appendChild(ul);
ulAlreadyAdded = false;
}
var li = document.createElement("LI");
li.class = "active";
li.id = "liID";
var clone = li.cloneNode(true);
document.getElementById("playlist").appendChild(clone);
a.href = e.target.result;
clone.appendChild(a);
//PLAYLIST CODE
var audio;
var playlist;
var tracks;
var current;
var len;
var link;
var par;
init();
function init() {
current = 0;
audio = $('audio');
playlist = $('#playlist');
tracks = playlist.find('li a');
len = tracks.length - 1;
audio[0].volume = .10;
// audio[0].play();
playlist.find('a').click(function(e) {
e.preventDefault();
link = $(this);
current = link.parent().index();
run(link, audio[0]);
});
audio[0].addEventListener('ended', function(e) {
current++;
if (current == len) {
current = 0;
link = playlist.find('a')[0];
} else {
link = playlist.find('a')[current];
}
run($(link), audio[0]);
});
}
function run(link, player) {
player.src = link.attr('href');
par = link.parent();
par.addClass('active').siblings().removeClass('active');
audio[0].load();
// Show loading animation.
var playPromise = audio[0].play();
if (playPromise !== undefined) {
playPromise.then(_ => {
// Automatic playback started!
// Show playing UI.
// We can now safely pause video...
audio.pause();
})
.catch(error => {
// Auto-play was prevented
// Show paused UI.
});
}
};
};
reader.readAsDataURL(file);
};
}
});
};
};
return ( < div > {
other()
} < /div>)
};
export default Download;
我已经被这个问题困扰了很长一段时间了。我的代码在控制台中运行,但页面上没有显示任何内容。有什么建议吗?
发布于 2019-11-29 21:48:23
尽管这个问题的主体似乎偏离了标题,但我将在标题中解决这个问题:如何将javascript文件导入到我的react组件中?
答案很简单。我选择将目录结构设置为:
./dummyDir
./dummyDir/react-src/
./dummyDir/react-src/src
这一切都可以在reactjs.org中看到。然后,所有特定于react/javascript的代码都将放入.../src/
中。因此,您将拥有./dummyDir/react-src/src/myJSFile.js
还有两个步骤。下一步是确保将javascript和任何类(假设您做了一些oop操作) import
到.jsx
文件中。现在,您可以直接在react组件中调用旧的javascript文件中的任何方法。
最后一步是托管和部署。我不会像在本地环境中那样去托管。例如,我在本地机器上使用apachectl测试主机。如果你是这种情况,你可能会因为不从npm run serve
托管而挂起,因为看起来你正在尝试插入react而不是从react开始。
因此,您需要一个包含以下内容的bash脚本:
deploy () {
if [ ! -d build/ ]
then
echo -e "ERROR:\n\tDoes not appear to be correct directory;"
echo -e "\tDir /build/ not found;"
return 1
fi
echo -e "STARTING:"
# Testing if build failed...
npm run build
if [ ! $? -eq 0 ] ;
then
printf "\nnpm run build has failed...\n"
printf "\npreserving previous ./build/\n"
return 2
fi
if [ -d ../static/ ]
then
echo -e "\tREMOVING ../static/*"
rm -r ./../static/ || echo "\tWARNING: DID NOT REMOVE DIR"
else
echo -e "\tDID NOT FIND ../static/ FOR REMOVAL"
fi
# Redirecting the ls output to make it completely silent
if ls ../precache-manifest* 1> /dev/null 2>&1;
then
echo -e "\tREMOVING ../precach*"
rm -r ./../precache-manifest* || echo "\tWARNING: DID NOT REMOVE CACHE"
else
echo -e "\tDID NOT FIND ../static/ FOR REMOVAL"
fi
echo -e "\tRUNNING: npm run build"
echo -e "\tCOPYING OVER NEW BUILD DIR"
cp -r build/* ../
echo -e "DONE"
return 0 #Bash is weird; 0 is true, 1 is false;
}
https://stackoverflow.com/questions/59110919
复制