4

I want to convert a blob URL AKA (window.URL.createObjectURL(blob);) into a file object so I can use it with FormData() so I can use that as an upload image file for AJAX but I am not able to do that successfully and I can't find a way to make the blob URL into a file

object for my code situation and I know its possible to do this according to the posts I visited on here it can be done here's one of posts that claim that you can do that How to convert Base64 String to javascript file object like as from file input form? but the reason why I'm not using any of those posts methods because I don't know how to integrate their methods to my code situation or its too complicated to understand.

This is my code that I been working on.

index.php

<script>

document.addEventListener('DOMContentLoaded',function(){

document.querySelector('#image-input').addEventListener('change',createABlobUrlForAImgSrcAndUseThatAsAFileUploadFile);

function createABlobUrlForAImgSrcAndUseThatAsAFileUploadFile(){

//Creating a blob URL

var image_input = document.querySelector('#image-input').files[0];

var file_type= image_input.type;

var blob = new Blob([image_input], { type: file_type || 'application/*'});

var blob_url= window.URL.createObjectURL(blob); //<-Example blob:http://localhost/ed6761d2-2bb4-4f97-a6d8-a35c84621ba5

//

//Form data
var formData= new FormData();

formData.append('blob_url', blob_url);
//

//<AJAX>
var xhr= new XMLHttpRequest();
xhr.onreadystatechange= function(){

if(xhr.readyState == 4){

document.querySelector('#output').innerHTML= xhr.responseText;

//<Allow JS in AJAX request>
var exJS= document.querySelectorAll('#output script');
var enableAll= exJS.length;
for(var i=0; i < enableAll.length; i++){
eval(exJS[i].text);
}
//</Allow JS in AJAX request>

}
}

xhr.open('POST','x');
xhr.send(formData);
//</AJAX>
}

});

</script>

<input id='image-input' type='file'>

<div id='output'></div>

x.php

<?php

$file=$_FILES['blob_url']['name'];
$location='images/'.$file;
move_uploaded_file($_FILES['blob_url']['tmp_name'],$location);  

?>

I know my code is not logically correct and I will have to change my code to be able to do what I want to do so I am aware it is not logically correct. Just trying to show you guys what I mean.

X 47 48 - IR
  • 1,250
  • 1
  • 15
  • 28

3 Answers3

4

This is how I got it done in my project. But in my case, I wanted to convert a blob to a wav file and then send to the back-end.

//Save your blob into a variable
var url = URL.createObjectURL(blob);

//Now convert the blob to a wav file or whatever the type you want
var wavefilefromblob = new File([blob], 'filename.wav');

//Pass the converted file to the backend/service
sendWavFiletoServer(wavefilefromblob);

//This is my function where I call the backend service to send the wav file in Form data
function sendWavFiletoServer(wavFile) {
  var formdata = new FormData();
  formdata.append("file", wavFile); 
  var ajax = new XMLHttpRequest();
  ajax.addEventListener("load", completeHandler, false);
  ajax.addEventListener("error", errorHandler, false);
  ajax.addEventListener("abort", abortHandler, false);
  ajax.open("POST", "https://yourserviceurl/api/");
  ajax.setRequestHeader('API_SECRET', UzI1NiIsInR5cCI6IkpXVCJ9eyLCJleHAiO');
  ajax.send(formdata); 
}
Ishara Amarasekera
  • 1,387
  • 1
  • 20
  • 34
  • Thanks for you reply and example I don't think your understanding what I want to do exactly I basically plan to use the blob urls in p tags for example like this

    blob:localhost/ed6761d2-2bb4-4f97-a6d8-a35c84621ba5

    and then get that blob urls from the p tag and covert it to a upload because I know a blob url is not a blob it's just a reference to a blob I just want to use the blob url that reference to a blob as a file input file I hope you know what I mean now . Use blob urls and get which blob url belong to what blob and convert it to a file input
    –  Jul 13 '19 at 09:13
  • document.querySelector('#a1).innerHTML; and convert the blob url to make a reference to its blob and convert it to a file upload –  Jul 13 '19 at 09:16
2

I had the same question and found a way.

This will give you a Blob object:

let blob = await fetch(url).then(r => r.blob());
  • While this is _probably_ slower / less-efficient than the examples where you are handling the blob in one code block, this works for me in my stack using React / Redux where I may have a component that handles picking the image and, another component further up the tree that handles uploading the image - Redux complains about File objects being `non-serializable` – Wombelite Feb 01 '23 at 01:24
1

I think uploading form data should be a blob object, not a blob URL

javascrip:

var image_input = document.querySelector('#image-input').files[0];
var blob_url= window.URL.createObjectURL(image_input); 
//Form data
var formData= new FormData();
// ...

// here , content-type: multipart/form-data
formData.append('upload_file', image_input);

php:

$file=$_FILES['upload_file']['name'];
$location='images/'.$file;
move_uploaded_file($_FILES['upload_file']['tmp_name'],$location);  
Chester
  • 722
  • 4
  • 6
  • 1
    Thanks for your feed back i'm not asking to send a blob url I basically want to convert the blob url on the client side into a file because I know that a blob url is just a reference to a blob because I plan to have blob urls on the page and then I want to get them like this for example

    blob:http://localhost/ed6761d2-2bb4-4f97-a6d8-a35c84621ba5

    and then use JS like this document.querySelector('#a1').innerHTML; and then convert it to a file upload because I know a blob url is just a reference to a blob I hope you understand what i'm trying to do.
    –  Jul 13 '19 at 09:03