Currently, I am working on a project to download videos from the browser using youtube-dl for educational purposes.
I have encountered an issue regarding downloading local mp4 files in the browser using axios. The download starts successfully; however, after it completes, I am unable to open the mp4 file.
Below is a snippet of my Go code:
func download(w http.ResponseWriter, r *http.Request) {
fileName := "video.mp4";
data, err := ioutil.ReadFile(fileName)
if err != nil {
log.Print(err)
return
}
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment; filename="+fileName)
w.Header().Set("Content-Transfer-Encoding", "binary")
w.Header().Set("Expires", "0")
http.ServeContent(w, r, fileName, time.Now(), bytes.NewReader(data))
}
Additionally, here is my JS function that executes when a user inputs text:
<script>
import axios from 'axios';
export default {
name: 'govideo',
data() { return {
url: '',
} },
methods: {
postreq() {
axios.post("http://127.0.0.1:8090/download", {
data: this.url,
responseType: 'blob'
}).then((response) => {
var fileURL = window.URL.createObjectURL(new Blob([response.data]));
var fileLink = document.createElement('a');
fileLink.href = fileURL;
fileLink.setAttribute('download', 'video.mp4');
document.body.appendChild(fileLink);
fileLink.click();
})
}
}
}
</script>
The issue seems to be with opening the downloaded video file in the browser. Should I consider making a separate get request for it?
Is there something missing or incorrect in my code that could be causing this problem?