skip to Main Content

I have a URL like this who corresponds to an image shown in a website:

https://firebasestorage.googleapis.com/v0/b/welook-siwe.appspot.com/o/moments%2F58810%2Ffe2c6769-af55-4fb3-a73f-4bac3bb2dbc3?alt=media&token=a123aaa3-7e77-4f1a-88cb-9f5169fd5b6e

I need to download that image but i can’t because it just downloads me this kind of file when i click on the URL: enter image description here

Is there any way to download that image using only the URL?

2

Answers


  1. You need to add Content-Disposition: attachment to the header of a file when uploading it to storage. This will tell the browser to download, not display the image in a browser.

    Example Firebase v9:

    import { getStorage, ref, uploadBytes, getDownloadURL } from 'firebase/storage'
    
    async function onChangeFileUpload(file) {
        const imageRef = ref(getStorage(), `images/${file.value.files[0].name}`)
        const metadata = {
            contentDisposition: 'attachment'
        }
        const result = await uploadBytes(imageRef, file.value.files[0], metadata)
        const doUrl = await getDownloadURL(result.ref)
        // Save downloadURL link in database.
    }
    

    Another way, if you want user to download any file by one click, you need to use <a href="" download>.

    Downloading file via link:

    <a href="https://example.com/link/to/your/file.jpg" download>Download File>></a>
    

    By the way, your link is lack of file extension .jpg.

    Login or Signup to reply.
  2. Changing the file name while saving

    If you want to download the image manually, Just download it, and save it as you_filename.jpg. When you add the extension, your file explorer is smart enough to decode the image, and you can see the thumbnail.

    Using Python Requests

    As the weblink have the media access token, you can download the image using simple get request. Make sure you write the file name with the file extension donwloadedimage.jpg as given below

    import requests
    
    def download_image(url):
        r = requests.get(url=url)
        path="downloadedimage.jpg"
    
        if r.status_code == 200:
            print("File Downloaded")
            with open(path, 'wb') as f:
                f.write(r.content)
                print("File Saved")
        else:
            print("Something went wrong")
    
    download_url="https://firebasestorage.googleapis.com/v0/b/welook-siwe.appspot.com/o/moments%2F58810%2Ffe2c6769-af55-4fb3-a73f-4bac3bb2dbc3?alt=media&token=a123aaa3-7e77-4f1a-88cb-9f5169fd5b6e"
    download_image(download_url)
    

    While Uploading

    Make sure you upload the image with the extension. If you upload without extension , Firebase will consider the object-type as not as image/jpg but as application/octet-stream

    When File Extension is not provided in Firebase

    enter image description here

    If you did that, reupload the image with right extension by renaming the file with right extension (jpg/jpeg/png).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search