Whenever canvas.toDataURL() is called, it will always provide the identical data

Greetings! I am currently in the process of developing an Ionic app with Firebase. My main goal is to upload multiple files while resizing them.

I have encountered a strange issue where, when I call the resize method, the input image appears different. However, once the uploading process is completed, it ends up uploading duplicated images (the last image in the array).

To investigate further, I used console.log to check the dataURL each time it resizes and discovered that the DataURL remains the same throughout.

The following code snippet demonstrates how to upload multiple files:

    multipleUpload: function(key, folder, files, targetWidth) {
    var q = $q.defer();
    var ct = Date.now();
    var urls = [];

    var recursive = function (n, args) {
      var arg = args[n];
      ImageService.resize(arg.file, targetWidth)
      .then(function(file) {
        upload(ct + '' + n + key, folder + '/' + key, file, CONFIG.MESSAGE.FILE_UPLOAD + (n + 1) + '번 파일')
        .then(function(url) {
          urls.push(url);
          if (++n < args.length) {
            recursive(n, args);
          } else {
            q.resolve(urls);
          }
        }), function(error) {
          q.reject(error);
        };
      })
    }
    recursive(0, files);

    return q.promise;
  },

Here's the code for the resizing method:

  resize: function(file, targetWidth) {
    var q = $q.defer();
    // Resizing Image
    var img = new Image();
    img.setAttribute('crossOrigin', 'anonymous');
    img.onload = function(){
      var canvas = document.createElement("canvas"),
      ctx = canvas.getContext("2d");
      canvas.width = targetWidth;
      canvas.height = canvas.width * (img.height / img.width);
      ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
      // Data URL to BLOB
      var dataURL = canvas.toDataURL();
      console.log(dataURL); // Returns same dataURL all the time.
      dataURLtoBlob(dataURL, Date.now())
      .then(function(blob) {
        q.resolve(blob);
      });
    };
    img.src = file;
    return q.promise;
  },

Answer №1

Initially, I noticed something strange - the function worked fine on iOS but not on Android.

I decided to investigate the plugin settings and disabled the allow-edit option, which then resolved the issue. It seems like there may be a problem with the plugin itself.

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

Testing an ngTagsInput element with Protractor: A step-by-step guide

I am currently facing an issue with testing a simple form using Protractor. The form works fine with basic input types, but I am encountering difficulties with ngTagsInput integration. I am looking for a way to set it up correctly without triggering the er ...

Generate a new JSON reply using the current response

I received a JSON response that contains values for week 1, week 2, week 3, and week 4 under the 'week' key, along with counts based on categories (meetingHash) and weeks. I attempted to merge this data using the .reduce method but without succes ...

Combining Firebase analytics with an Ionic 3 application using the Ionic Native plugin

I placed the GoogleService-Info.plist file at the root of the app folder, not in the platforms/ios/ directory. When I tried to build the app in Xcode, an error occurred in the following file: FirebaseAnalyticsPlugin.m: [FIROptions defaultOptions].deepLin ...

Submitting a Form with Multiple Pages

I'm encountering a challenge that I'm struggling to work through. Initially, we had a professional build our website, but since parting ways with the company, I've taken over site management. While I can handle basic tasks, I lack the expert ...

"Preventing Cross-Origin Requests" error encountered while trying to load a JSON document

I've been working on an online experiment using JavaScript, and I need to load parameters for the task from a JSON file. I managed to do this successfully when running the task through a live server. However, if I try to run it locally by opening the ...

Executing a separate function after each iteration of an ajax call within a loop using Jquery

I am faced with a situation where an ajax function must be run inside a loop that depends on the array's size. How can I execute another function after the ajax call is completed and outside the loop? Is this achievable? Below is the function: funct ...

How to deactivate choices in v-autocomplete

Is there a way to deactivate certain options in v-autocomplete? I tried using the item-disabled attribute and passing the string value of the option, but it didn't work as expected. <v-autocomplete :items="states" item-text="name" labe ...

Find a match by comparing a field only with arrays that contain at least one element

I'm currently working on a filtering system. Let me show you a simple version of my Order model: { merchant: <Reference to the merchant>, date: <Date Object> name: "Some order name", ingredients: [{ ingred ...

How can one obtain the object (whether it be a string or an array) named NAME?

Can someone help me create a prototype like this: Array.prototype.getName=function(){ [...]return arrayName; } Then I want to be able to do this: x = new Array; alert(x.name); and have "x" displayed in the alert. I'm currently developing on Chrom ...

ways to incorporate searching within JSON data using AJAX and jQuery in JavaScript

My search box needs to have JSON data appended into it. Below is the HTML code: <input type="search" id="merchantName" name="merchant" placeholder="enter merchant name"></input> I have JSON data containing merchant names that I want to appen ...

What is the process for incorporating an array of models?

let userData = await db.user.findOne({ attributes: attributes, include: ['charges', 'availability', 'practice',// 'services', 'education',// 'user_role', ...

AngularJS Issue [$injector:modulerr]

My AngularJS module, developed following these tutorials, is not working properly in conjunction with ASP.NET MVC. I have encountered an error [$injector:modulerr] and I am unsure of how to resolve it. (function () { var AAngScript = angular.module(&a ...

An unexpected error has occurred in the browser console: The character '@' is not valid

I recently made the decision to dive into learning about Unit Testing with JavaScript. To aid in this process, I started using both Mocha.js and Chai.js frameworks. I downloaded the latest versions of these frameworks onto my index.html from cdnjs.com. How ...

Utilizing Vuex state within Vue-Router route definitions

My Vuex store setup in main.js looks like this: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) //initialize the store const store = new Vuex.Store({ state: { globalError: '', user: { ...

Angular: encountering an issue with reading the property 'then' of undefined object

My app has a service in place to upload images to Amazon S3 after signing them with my backend using the Cordova file-transfer plugin. I trigger this service after capturing a picture with the Cordova camera plugin to upload the captured image to the S3 b ...

What could be causing my function to return undefined instead of an array?

I have been working on a function to query my database and retrieve specific details for the selected item. While it successfully finds the items, it seems to be returning undefined. var recipefunc = function(name) { Item.find({name: name}, function ...

Searching for the position of different size values according to their specific value

information = { boxNoTo: 1, boxNoFrom: 1, size: 'M', } items = [{ size: 'M', },{ size: 'M', },{ size: 'S,M,L,XS', boxNoTo: 1, boxNoFrom: 1, country: 'CA', name: 'Josh' }] This is what I have don ...

Server-side props become inaccessible on the client side due to middleware usage

I'm attempting to implement a redirect on each page based on a specific condition using Next.js middleware. Strange enough, when the matcher in middleware.ts matches a page, all props retrieved from getServerSideProps for that page end up being undef ...

Automatically update the selection in an md-select dropdown

I am dealing with the following HTML structure <md-select id="testSelect" ng-model="objElements" ng-change="onElementChange()" name="objElements" placeholder="Select Stuff"> <md-option id="testOption" ng-r ...

Is it possible to utilize $save as $add in Firebase with Angular Fire?

Is it possible to utilize $save instead of $add in Firebase object for adding data? ...