File size: 1,207 Bytes
15975c4
 
 
 
 
 
 
 
d909077
 
 
15975c4
 
 
d909077
 
 
15975c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React, { useState } from 'react';
import axios from 'axios';


const AudioDownloader = () => {

  const [downloadUrl, setDownloadUrl] = useState<string | null>(null);

  const fileId = localStorage.getItem('uploadId');
  console.log('Retrieved Upload ID:', fileId); 


  const handleDownload = async () => {
    try {
      const server = import.meta.env.VITE_API_URL;

      const response = await axios.get(`${server}/download/${fileId}`);
      
      // Assuming the response.data contains the URL
      const url = response.data.url;

      

      // Set the download URL in the state
      setDownloadUrl(`${server}/${url}`);
    } catch (error) {
      console.error('Error fetching download URL:', error);
    }
  };

  const handleClear = () => {
    // Clear the download URL from the state
    setDownloadUrl(null);
  };

  return (
    <div>
      <button onClick={handleDownload}>Download Audio</button>
      {downloadUrl && (
        <div>
          <a href={downloadUrl} download={`downloadUrl`}>
            Download Link
          </a>
          <button onClick={handleClear}>Clear Download Link</button>
        </div>
      )}
    </div>
  );
};

export default AudioDownloader;