OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

ReferenceError: File is not defined - express

  • Thread starter Thread starter Maya Rahto
  • Start date Start date
M

Maya Rahto

Guest
I`m trying to convert some base64 string to a image file and pass it to firebase via express.

Everything works fine on front end, except this part:

Code:
const convertBase64ToFile = (base64String, fileName) => {
  let arr = base64String.split(',');
  let mime = arr[0].match(/:(.*?);/)[1];
  let bstr = atob(arr[1]);
  let n = bstr.length;
  let uint8Array = new Uint8Array(n);
  while (n--) {
    uint8Array[n] = bstr.charCodeAt(n);
  }
  const file = new File([uint8Array], fileName, { type: mime }); /// getting Error in this line
  return file
}

Which library i have to import?

Error:

Code:
  const file = new File([uint8Array], fileName, { type: mime }); /// getting Error in this line
               ^

ReferenceError: File is not defined
    at convertBase64ToFile (C:\Users\rahto\devel\new\maissaudeapi\api\firestore\write.js:19:16)
    at conversor (C:\Users\rahto\devel\new\maissaudeapi\api\firestore\write.js:33:16)
    at C:\Users\rahto\devel\new\maissaudeapi\mainServer.js:31:21
    at Layer.handle [as handle_request] (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:284:15
    at Function.process_params (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:346:12)
    at next (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:280:10)

Node.js v18.6.0
[nodemon] app crashed - waiting for file changes before starting...

Then, i changed to this:

Code:
const convertBase64ToFile = (base64String, fileName) => {
  let arr = base64String.split(',');
  let mime = arr[0].match(/:(.*?);/)[1];
  let bstr = atob(arr[1]);
  let n = bstr.length;
  let uint8Array = new Uint8Array(n);
  while (n--) {
    uint8Array[n] = bstr.charCodeAt(n);
  }
  const file = fs.writeFileSync(fileName, uint8Array)
  let fiz = fs.readFileSync(fileName, file);
  // const file = new File([uint8Array], fileName, { type: mime });
  return fiz
}

And got this error:

Code:
C:\Users\rahto\devel\new\maissaudeapi\node_modules\@firebase\storage\dist\index.node.cjs.js:3036
    const newPath = child(ref._location.path, childPath);
                                        ^

TypeError: Cannot read properties of undefined (reading 'path')

<p>I`m trying to convert some base64 string to a image file and pass it to firebase via express.</p>
<p>Everything works fine on front end, except this part:</p>
<pre class="lang-js prettyprint-override"><code>const convertBase64ToFile = (base64String, fileName) => {
let arr = base64String.split(',');
let mime = arr[0].match(/:(.*?);/)[1];
let bstr = atob(arr[1]);
let n = bstr.length;
let uint8Array = new Uint8Array(n);
while (n--) {
uint8Array[n] = bstr.charCodeAt(n);
}
const file = new File([uint8Array], fileName, { type: mime }); /// getting Error in this line
return file
}
</code></pre>
<p>Which library i have to import?</p>
<p>Error:</p>
<pre><code> const file = new File([uint8Array], fileName, { type: mime }); /// getting Error in this line
^

ReferenceError: File is not defined
at convertBase64ToFile (C:\Users\rahto\devel\new\maissaudeapi\api\firestore\write.js:19:16)
at conversor (C:\Users\rahto\devel\new\maissaudeapi\api\firestore\write.js:33:16)
at C:\Users\rahto\devel\new\maissaudeapi\mainServer.js:31:21
at Layer.handle [as handle_request] (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:284:15
at Function.process_params (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:346:12)
at next (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:280:10)

Node.js v18.6.0
[nodemon] app crashed - waiting for file changes before starting...
</code></pre>
<p>Then, i changed to this:</p>
<pre class="lang-js prettyprint-override"><code>const convertBase64ToFile = (base64String, fileName) => {
let arr = base64String.split(',');
let mime = arr[0].match(/:(.*?);/)[1];
let bstr = atob(arr[1]);
let n = bstr.length;
let uint8Array = new Uint8Array(n);
while (n--) {
uint8Array[n] = bstr.charCodeAt(n);
}
const file = fs.writeFileSync(fileName, uint8Array)
let fiz = fs.readFileSync(fileName, file);
// const file = new File([uint8Array], fileName, { type: mime });
return fiz
}
</code></pre>
<p>And got this error:</p>
<pre><code>C:\Users\rahto\devel\new\maissaudeapi\node_modules\@firebase\storage\dist\index.node.cjs.js:3036
const newPath = child(ref._location.path, childPath);
^

TypeError: Cannot read properties of undefined (reading 'path')
</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
3
Total visitors
3
Top