0

I am creating a web app, in which I receive a image file from the user then have to store it in my Firebase Realtime Database and display it in Html. Since Realtime Database doesn't support files I have to create a Url for my image.

The Url I want is not a download link like this which I got from the Firebase Cloud Storage Url creation tool, rather I need a viewable link like this which I can add to the src on my tag in Html like this:

<img src="The Url">

I have tried using Firebase Cloud Storage, but when uploading and image file, the image is uploaded as an Application/octate-stream rather than an image. When I run the geturl() function it gives me this link which leads to a download page and therefore I cannot it to the src attribute of tag.

The file uploading code is this:

var file_from_usr = document.getElementById('file_input')
var storageRef = firebase.storage().ref();
var thisRef = storageRef.child(filename);
thisRef.put(file_from_usr).then((snapshot) => {
    console.log('Uploaded a blob or file!');
});
2
  • convert your image binary data into base64 string Commented May 5, 2021 at 4:54
  • how can I do it? Commented May 5, 2021 at 4:58

2 Answers 2

0

This seems like a metadata issue to me. Your code is not able to infer the file's type, so by default, it will assume it to be an Application/octet-stream content type.

if for some reason your filename has no extension or some other problems then you can manually specify that content-type metadata.

var metadata = {
  contentType: 'image/jpeg',
};

// Upload the file and metadata
var uploadTask = storageRef.child('images/mountains.jpg').put(file, metadata);

https://firebase.google.com/docs/storage/web/upload-files

Alternatively, try putting an extension to your filename . eg. filename= "myimage.jpg"

Sign up to request clarification or add additional context in comments.

2 Comments

Using this the file gets uploaded as an image but then I think it gets corrupted. This is the link I get
Try uploading a different image. Also , Converting your image to base64 encoding (text formatis not an efficient way to upload files.
0

download image data using fetch api and convert it into url which you can set to image src attribute; Added the code snippet for the same below. This code is to download image and skipped the upload task which seems like working as mentioned in above post.

getImageURL(url) {
  const options = {
    method: 'GET',
    mode: 'no-cors',
    cache: 'default'
  };
  return fetch(url, options).then((response) => {
    return response.blob();
  }).then(blob => {
    return URL.createObjectURL(blob);
  });
}

document.getElementById("myImg").src = getImageURL("https://{your_url_path}");

9 Comments

Sorry I am not able to understand what are you trying to say. I am a new to Javascript. How can I download image data using fetch api and convert it into a url?
you need to call getImageURL() method by passing the url, and it will get the response as blob and will convert to url.
Oh ok. Now I get it. I will try and respond
This is giving me this error: Access to fetch at 'firebasestorage.googleapis.com/v0/b/…' from origin '127.0.0.1:5500' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
you need to disable cors to make it work from your local machine
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.