What is the process for uploading a cropped image using vue-cropper.js?

Currently, I am utilizing an npm package known as vue-cropper.js for image cropping and subsequent upload to a server. However, I am encountering an issue with the uploading process of the cropped image due to a CORS error. Interestingly, when I attempt to upload an uncropped image, there are no errors and it works perfectly. This inconsistency has left me puzzled about where I could be going wrong.

Despite reviewing the cropper.js documentation multiple times, the instructions provided involve creating a blob and appending it to form data followed by a POST request. Surprisingly, this is exactly what I have been implementing.

Below is the template for the cropper component:


    <vue-cropper
      v-show="imageSrc"
      class="vue-cropper"
      ref="cropper"
      :guides="true"
      :view-mode="1"
      drag-mode="crop"
      :background="true"
      :rotatable="true"
      :aspect-ratio="1"
      :src="imageSrc"
      alt="Image"
    ></vue-cropper>

Furthermore, here is my code snippet responsible for the cropping and uploading functionalities:

crop() {
      this.$refs.cropper
        .getCroppedCanvas({
          width: 200,
          height: 200
        })
        .toBlob(blob => {
          const formData = new FormData();
          formData.append("photo", blob, 'avatar');
          this.uploadImage(formData);
        });
    },
    rotate() {
      this.$refs.cropper.rotate(90);
    },
    uploadImage(formData) {
      const token = getCookie("ifragasatt_cookie");
      const url = "https://vem-user.fjardestatsmakten.se/userProfilePic";
      const headers = {
        "Content-Type": "multipart/form-data",
        "Access-Control-Allow-Origin": "*",
        "ifr-jwt-token": token
      };

      axios.post(url, formData, { headers });
    }
  }

My expectation is to receive an "ok" response from the endpoint after successful upload. However, the actual result shows:

POST https://vem-user.fjardestatsmakten.se/userProfilePic 502

my-profile:1 Access to XMLHttpRequest at 'https://vem-user.fjardestatsmakten.se/userProfilePic'
from origin 'http://localhost:8080' has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.

Answer №1

Have you implemented CORS in your back-end code? I noticed an "Access-Control-Allow-Origin" error in the POST response. Also, have you converted the image from base64 to a blob format?

Answer №2

Success! I managed to fix the issue by adding '.png' to the 'avatar' file.

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

Extracting video frames in three.js for future reference

Currently, I am utilizing three.js version r73 and have implemented a fragment shader that showcases video frames with code like this: gl_FragColor = texture2D( iChannel0, uv); My objective is to introduce another uniform (let's call it iChanne ...

Tips for maintaining specific attributes in every object retrieved from a fetch request

When making a fetch call, I received an array containing over 100 objects, each with around 10 properties. However, I am only interested in keeping 3 properties for each object. Here is an example of the returned JSON: [ { id: 'base1-1', ...

What measures can be taken to prevent the reloading of a subfolder within the same parent in the Fuel

var DataSourceTree = function (options) { this.source = options.source; } DataSourceTree.prototype = { data: function (options, callback) { var self = this; var ...

I'm attempting to render HTML emails in ReactJS

I have been attempting to display an HTML page in React JS, but I am not achieving the same appearance. Here is the code snippet I used in React JS: <div dangerouslySetInnerHTML={{ __html: data }}/> When executed in regular HTML, the page looks lik ...

I'm feeling lost trying to figure out how to recycle this JavaScript function

Having an issue with a function that registers buttons above a textarea to insert bbcode. It works well when there's only one editor on a page, but problems arise with multiple editors. Visit this example for reference: http://codepen.io/anon/pen/JWO ...

Issue with Bootstrap datepicker functionality within a modal

I've searched extensively on Google and Stack Overflow for a similar question, but so far haven't been successful. The issue I'm facing is when I create and open a modal from jQuery and try to access a date picker, the datepicker's Java ...

"Error encountered: Array is undefined when using the map and subscribe functions in Ionic

I have developed a service that is supposed to retrieve data from a JSON file and assign it to an array called 'countries', which will be used throughout the application on multiple pages. However, when I call the method getCountries, the countri ...

Transmitting a JSON string to my backend system to insert into my database, but unfortunately, no data is being added

I've been facing a challenging issue with my code Currently, I am attempting to insert an object into my database using jQuery/AJAX. Despite not encountering any errors, the data is not getting added to my DB. Here is the snippet of my JS/JQuery cod ...

Create basic HTML using the react.cloneElement method

When using React.cloneElement(), the first parameter always needs to be a react component that is passed as children in props. Is there a way to pass a simple HTML node as a child? Please see the example code below for clarification: Dialog.jsx (Common c ...

Erase the contents of a div by clicking a button

I am currently working on a game project that utilizes drag-and-drop functionality, similar to Blockly and Scratch. One of the key features I am trying to implement is the ability to clear the contents of a target container by clicking a Reset button. Desp ...

a guide on using ajax to distribute retrieved data among various td elements

After the user presses a button, the data is saved into the database. Upon success, I need to retrieve the data from the database and place it in the appropriate td (in the same row as the clicked td). I've been able to successfully retrieve the data, ...

Encountering Vue Router Error while integrating Laravel 9 with Vite and Vue CLI 3

I am struggling to fix it, please assist me. I can't seem to pinpoint the issue and need some guidance. Error Image Links: https://i.stack.imgur.com/VhVXL.png https://i.stack.imgur.com/IENFm.png ...

Is there any specific value that will always result in a true comparison in JavaScript?

Is there a special JavaScript value that will always make a comparison true? For example using the less than operator: true < 10 true false < 10 true null < 10 true Or using the greater than operator: true > 10 ...

What exactly is the purpose of the colon in JavaScript's import statement?

Looking at the following example. import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database' I am puzzled by the use of colons and I am unsure about where the imported files are being referenced from. ...

I'm looking to use node.js to post an image on my blog

I am currently in the process of setting up a new blog using node.js. Everything is working smoothly on my local setup, but I am facing an issue when it comes to adding images to my articles. Despite utilizing bootstrap for input display, I am unable to sa ...

Shared codes are compatible with both C and Javascript programming languages

Within the scope of this project, data will be retrieved from the server in either JSON or XML format. There are two separate client applications that will handle the data processing, with one being web-based and written in JavaScript, while the other is ...

Creating a web application that efficiently stores and retrieves user preferences

Currently, I am working on developing a Nodejs/Express web application that is similar to a dashboard or homepage for a web browser. The data displayed on the page is customized based on the user's settings. When a user signs up, I create a json file ...

What could be the reason for Element-plus not functioning properly within storybook?

Following the documentation guidelines, I successfully set up storybook. The configuration file was modified as required to integrate storybook with Vite, supporting the use of the element-plus library. In .storybook/main.js: module.exports = { stories: ...

Discovering the process to verify and obtain the outcome of an API request through jQuery Ajax

Seeking assistance in utilizing jQuery AJAX to access an API with a URL and parameters using the POST method. Specifically, I aim to determine the availability of delivery for a given Pincode on an e-commerce website. Any guidance on how to retrieve data f ...

Navigate through PNG image - proceed only if the clicked area is transparent

Imagine having an image that looks like this: https://i.sstatic.net/Ab6aW.jpg I am looking to make it so that when I click on the transparent areas of the image, the click should pass through to the element below. However, if I click on a non-transparent ...