Implement a feature that allows a user to upload multiple files from a Uint8ClampedArray using

After creating a binary file in the Browser using JavaScript as Uint8ClampedArray, I am now faced with the task of uploading it to a webserver as if it was selected from a file-picker.

My attempt at this resulted in the following code:

var data = new Uint8ClampedArray(32);
data[0] = 0x42;
data[1] = 0x4D;
postdata = new FormData();
postdata.append('data', new Blob(data), 'test.txt');
fetch('http://localhost/',{
    method: 'POST',
    body: postdata
});

However, the request generated looks like this:

POST / HTTP/1.1
content-length: 230
content-type: multipart/form-data; boundary=----WebKitFormBoundaryfsPRCG3QGnD2bWZS

------WebKitFormBoundaryfsPRCG3QGnD2bWZS
Content-Disposition: form-data; name="data"; filename="test.txt"
Content-Type: application/octet-stream

6677000000000000000000000000000000
------WebKitFormBoundaryfsPRCG3QGnD2bWZS--

And unfortunately, it results in this Textfile being created :(

6677000000000000000000000000000000

I'm looking for guidance on how to properly create a valid binary Blob(). Thank you!

Answer №1

Give this method a shot,

let data = new Uint8ClampedArray(32);
data[0] = 0x42;
data[1] = 0x4D;
formData = new FormData();

// When using the Blob constructor, make sure to pass an array like `[data]` instead of just `data`
formData.append('data', new Blob([data], {type: "text/plain"}), 'test.txt');

fetch('http://localhost/',{
    method: 'POST',
    body: formData
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtain an Instagram access token with the help of Node.js

I've been attempting to acquire an access token from Instagram without utilizing a browser request, using the instagram-node module. However, I haven't been successful in receiving the access token. Can anyone provide me with a solution that does ...

How to Insert PHP/MySql Data into JavaScript

As I delve deeper into PHP OOP, I feel like I'm making progress in transitioning my website. Currently, each user has their own page with a javascript grid tailored to their specific needs. For easier maintenance, I'm considering the idea of havi ...

Why is my AngularJS li scope not being removed?

Can someone please assist me? I am new to AngularJS and need help with a function I created. I have created a function that adds a next tab section and removes the current section. Similarly, I have another function that adds a previous tab section and re ...

What is the best way to have one modal trigger the opening of another modal in Angular 4+

On my main webpage, there is a modalA (form) that appears when clicked. Within this modalA, there is a button that triggers the opening of another modalB (form). If I decide to cancel the modalB, I want it to return to modalA instead of going back to the ...

Any ideas on how to fix the error that pops up during the installation of the bootstrap package in Node

The npm command is not recognized as a valid cmdlet, function, script file, or operable program. Please double check the spelling of the command and ensure that the path is correct before trying again. This error occurred at line 1. npm i bootstrap + ...

Creating a Custom Alert Box in Javascript with Image Alignment

I have just finished creating a custom alert box using Javascript, complete with text and images. However, I am facing an issue with alignment. The output is not as expected. I am trying to display the correct mark and text on the same line. Can someone ...

Is there a way to manipulate the checkbox using the filter?

I'm struggling to create a controllable checkbox that will be checked if the post id matches an id from another array. I want the checkbox to add the post id to a bookmark array when clicked, and I need it to be controllable. The Redux store provides ...

"The program developed in Php/Ajax/jQuery/Javascript functions flawlessly on one server but encounters challenges on a different host. The mystery unrav

element, I'm encountering an issue that doesn't seem to be related to any specific code problem. There are no error messages indicating what steps I should take next. Here's the problem: I have a script similar to Facebook's wall feat ...

Remove specific data regardless of letter case from the row

I've developed a Google Apps Script that scans specific columns for certain keywords and deletes rows that contain them: function filterRows() { var sheet = SpreadsheetApp.getActiveSheet(); var rows = sheet.getDataRange(); var values = rows.g ...

The HTML unit is unable to locate the form for unknown reasons. My suspicion is that it could be due to the presence of Angular

Struggling with using HTMLunit to login and upload a file. Successfully logged in but unable to locate the form, despite it being present on the HTML page. Here is the JAVA CODE snippet: public static void main(String[] args) { WebClient webClient = ...

When using Jest, the mongoose findOneAndUpdate function may return null values for both error and document

I've been struggling with Mongoose's findOneAndUpdate method as it doesn't seem to provide any useful information. I have tried accessing the Query returned from calling it (stored in updatedUser), but all it returns is null. Adding a callba ...

Accessing data stored in XML or JSON files from a local server

I am currently working on loading a list of coordinates for a Google map from either an XML or JSON file, both of which are hosted in the same directory on my local test server. So far, I have used a hard-coded JSON object to load map coordinates for tes ...

Should I choose JavaScript or TypeScript for Angular 2.0?

What is the best approach for creating an Angular 2.0 application? Should it be done with JavaScript or TypeScript? I'm struggling to get started with Angular 2.0 using TypeScript, as it's quite different from JavaScript. ...

Is it possible to replicate a slow image loading experience in React.js?

Referenced the solution below How to simulate slow loading of image Slowing down internet connection with Chrome DevTools works, but the entire site loads slowly. I want to specifically focus on slowing down the image reloading. One of the suggestions w ...

Error encountered while attempting to send a delete request to MongoDB due to connection refusal

Recently, I've been diving into a Next.js tutorial that involves working with MongoDB. Everything seems to be running smoothly when testing my API endpoints with Postman. POST, GET, and DELETE requests all go through without any hiccups. However, thi ...

Integrate internal JavaScript and style files into a Google Apps Script

Currently, I am working on a Google Apps Script project that consists of a *.gs-file and a *.html-file. This project is being developed in the browser on script.google.com, which means all files are stored within my Google account. In the html file, I have ...

Updating the content of a window without the need to refresh the page using JavaScript

Is there a way to navigate back to the previous window in chat_user without refreshing the entire page when the back button is clicked? Below is the code I have tried: <a href="" onclick="window.history.go(-1); return false;">back</a> ...

What is causing the required check to fail even after removing an element?

Can you help me figure out why my required check is not working in autocomplete when using material UI with react hook form? Here are the steps to reproduce: Click on the Submit button, it should show that the field is required. Select any element from t ...

Transform my Curl script into a NodeJS app

I'm trying to replicate the functionality of a curl command within my NodeJS application, but I am facing some difficulties. Any guidance on how to achieve this would be greatly appreciated. curl -H "Authorization: bearer $TOKEN" If I already hav ...

Tips for efficiently moving through divs in a personalized single page application?

Exploring the world of JS frameworks and single page app functionality is a bit of a mystery to me. Currently, I have a pseudo single page app set up without any specific framework in place. The setup involves 3 tabs that toggle visibility for 3 different ...