-1
なぜこのコードが機能していないのか分かりません。私が持っているのは、コンポーネントが受け入れるファイルタイプの配列です。その配列を繰り返し処理し、アップロードされるファイルがこれらの型のいずれかであるかどうかを確認します。そうでなければ、私はreturn;
です。何らかの理由でacceptedFormats.forEach
のコードが引き続き実行されます。 console.log
audioType
とfile.type
の場合はそれぞれINVALID
が得られます。だから、それは戻ってくるべきではないでしょうか?ありがとう!関数は返されませんか?
class Upload extends React.Component {
state = {
percentage: 0,
uploading: false
}
handleUpload = (e) => {
if (this.state.uploading) {
return;
}
const file = e.target.files[0];
const acceptedFormats = ["audio/wav", "audio/mp3", "audio/m4a"];
// TODO: Figure out why this isn't working
acceptedFormats.forEach((audioType) => {
console.log(audioType);
console.log(file.type);
if(audioType !== file.type) {
console.log('INVALID');
return;
}
})
const audioRef = storageRef.child('audio/' + file.name);
const task = audioRef.put(file);
task.on('state_changed', function(snapshot){
const progress = (snapshot.bytesTransferred/snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
this.setState(() => ({
percentage: progress
}))
}.bind(this), function(error) {
}, function() {
const downloadURL = task.snapshot.downloadURL;
const fileName = task.snapshot.metadata.name;
let songName = removeFileExtension(file.name);
songName.replace(/\./g,' ');
db.ref(`users/${this.props.uid}/uploadedSongs/`).push({
downloadURL,
fileName,
songName,
}).then(() => {
this.setState({
percentage: 0,
uploading: false
})
Alert.success(`Upload successful`, {
position: 'top-right',
effect: 'slide',
timeout: 3000
})
})
}.bind(this));
}
render() {
return (
<div className="Upload">
<label className={this.state.uploading ? "Upload__button-disabled btn" : "Upload__button btn"}>
<span className="Upload__button-text">{this.state.uploading ? "Uploading" : "Upload a song"}</span>
<input className="Upload__input" type="file" onChange={(e) => this.handleUpload(e)} disabled={this.state.uploading ? true : false}/>
</label>
<span style={{color: '#808080', fontSize: '12px'}}>Accepted formats: .wav, .mp3, .m4a</span>
</div>
);
}
}
ReactDOM.render(<Upload />, document.querySelector('#u'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.0/react-dom.min.js"></script>
<h1>Example Upload</h1>
<div id="u"></div>
.some()
呼び出しから返された場合return
からBoolean
true
またはfalse
とif
条件と文を返し.some()
を使用することができます。私はタイプごとにINVALIDを取得します。これらのものをログに記録しても、「INVALID」は返されません。あなたは 'if(audioType!== file.type)'ブロックの中に入ると言っていますか? – jmargolisvtこれは純粋なJavascriptですか? Javascriptでは、あなたが今やっているのと同じようにキーワード「extend」を受け入れますか?値を返すか、プロセスを停止するだけですか? – smzapp
私はそれをきれいにして、その実行可能な...これは明らかにReact.jsなので、私はタグを付け加えました – styfle