Using nuxt.js to Import Files (Harnessing the Power of Vue's Equivalent to getElementById())

I'm struggling with loading a file in one of my components after transitioning to Vue from using getElementById(). Here's what I've attempted so far: In the template:

<input type="file" accept=".obj" id="file" ref="OBJImport" name="objFile" @change="loadFileAsText()" single />

In data:

data(){
    return {
         files: []
    }
}

In methods:

methods: {  
    loadFileAsText: function() {
        if(process.client) {
            this.files = this.$refs.OBJImport.files[0];
            console.log(files);
         }
    }

However, when attempting to upload the file, I encounter the following error:

ReferenceError: files is not defined

I have also tried eliminating the [0]. Making the switch from .files[0] to .file results in the same error for files. What steps should I take to successfully pass my file from the template to my methods? Appreciate any assistance!

Answer №1

Eliminate the use of () in the change event:

<input type="file" accept=".obj" id="file" ref="OBJImport" name="objFile" @change="loadFileAsText" single />

Utilize the default event parameter

methods: {  
  loadFileAsText: function(e) {
    if(process.client) {
      //this.files = this.$refs.OBJImport.files[0];
      //console.log(files); // This is where your error lies - using files instead of this.files.
      this.files = e.target.files[0];
      console.log(this.files);
  }
}

The files that you are logging does not actually exist, which is your main mistake. Furthermore, I have provided an alternative method for fetching files.

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

Exploring the power of "then" in AngularJS promises: Jasmine's journey

In my AngularJS controller, I have the following function: service.getPlaceByAddress = function(address) { return $q(function(resolve, reject) { geocoder().geocode({'address': address}, function(result, status) { // gets ...

The jQuery file that is included in the main master page is not accessible for the .aspx page that inherits the same master page

In my web development project, I am facing an issue with including the jQuery file in a master page. The structure of my project is as follows: The master page is located in the root directory. The jQuery file is stored in a folder named "script". The sp ...

What is the best way to substitute unpredictable dynamic variables on-the-fly?

I am working with a .js file that contains a config structure similar to this: genGetLocations:{ data_url:'restaurants/{var1}/tables/{var2}, } This is just one example. Some configurations may have data_url with more than two dynamic variables. I ...

Transforming Jquery into vanilla JavaScript and sending a POST request

Hey everyone, I'm in the process of converting Jquery code to pure javascript and encountering some difficulties with the following snippet: $.post('ajax_data',{action:'service_price',service:service,quantity:quantity,dripfeed:drip ...

Using jQuery to assign a specific value to all select boxes

I am facing a challenge where I need to change the values of all select boxes on my page to a specific number. Here is the HTML structure: <select> <option value="-1" <option value="55">ENABLE</option> <option value= ...

Discovering the amount of time a section of a webpage remains in view on a browser

I am looking for a way to track user engagement on my website that contains pages with extensive scrolling and numerous images. Each user who logs in sees a unique selection of images. My goal is to monitor how long users linger on each part of the page t ...

Present location of current page

I utilize angularJS to create a web page with the utilization of ngView for a single page and multiview. Is there a way to display the current page's location, such as Home/abc/dcv? On this website https://docs.angularjs.org/guide/, the page location ...

Transferring data between two "Data" elements using Jquery Datatable

Utilizing the JQuery datatable, I have made the first column of my table clickable, which is labeled as RequestNo. Here's the code snippet: "ajax": { "url": "/Request/Search/LoadData", "type": "POST", "datatype": "j ...

AngularJS - Triggering functions on image load event

I have been on a quest to find the best way to handle the onload event for images in Angular using jqLite. I came across this question, but I am looking for a solution that involves directives. Therefore, the approach below is not satisfactory to me: .c ...

Trouble mounting nested components in Vue.js: Component failed to mount due to undefined template or render function

I encountered an error while using Vue-CLI within the <comment> component. The issue arises when the submitComment() method of CommentForm is executed, causing the comment object to be added to selfComments for rendering. It appears that there might ...

Guide on extracting data from a JavaScript table using Python

Looking to extract information from a table on this page: . There are a total of 18 pages, but the url remains constant for each page. My usual method involves using BeautifulSoup to scrape data from HTML pages. However, in this case, the required data is ...

When trying to click on an HTMLTableRowElement, an Uncaught ReferenceError occurs in React.js and jQuery, stating that the function is

When I was working on my web app, I encountered an issue while trying to create a table. Upon clicking on it, I received an error message: Uncaught ReferenceError: (function) is not defined at HTMLTableRowElement.onclick Here is the code for the table: $( ...

Pass the callback function `cb_func` as a parameter to the `foobar` function when calling it in the `onclick`

I have an element that I want to trigger an onclick event with a callback function. Here is the code snippet: function cb_func() { alert("hello world!"); } function foobar(i, cb) { // do something if (cb != undefined) cb(); } onclic ...

Retrieving User Keypad Input with Twilio Phone Calling in a Node.js Application (excluding web server)

const userInput = message.content.split(' ') const phoneNumber = userInput[1] const messageToSay = userInput.slice(2).join(' ') if (phoneNumber) { // Dial phoneNumber and deliver messageToSay, then gather ke ...

Unable to locate router-link and router-view components

router-view and router-link components seem to be causing some issues in my app. I'm not sure what the problem is. I have included the code for the app below. It's a simple homepage app with a navigation bar (like a navbar) to navigate to differ ...

Steps for creating checkboxes for individual table rows in HTML using embedded PHP and updating their values in a PostgreSQL database:1. Begin by iterating through each

I have already retrieved the data from the database. It is displayed on the HTML table, but it is currently in non-editable mode. I need to ensure that the data shown is accurate and update its Boolean value in the database. Otherwise, incorrect records sh ...

Transforming numerical figures into written form within a specified range: A step-by-step guide

I have a unique custom range design that I have personally customized. Beneath each step of the range, there is a value displayed in a stylish green box. https://i.sstatic.net/vzeRK.png My query is regarding how I can replace the numeric values with tex ...

Managing a large number of records in a for loop on a Node.js server can be challenging, especially when dealing with nearly

After setting up a NodeJS server and connecting it to a MySQL database with around 5000 users, I needed to read the data from MySQL and update a MongoDB database. I managed to write code for this process. https://gist.github.com/chanakaDe/aa9d6a511070c3c78 ...

Why is it necessary to decode JSON stringified objects in Vue props?

When passing a stringifyed object via props to a component, it seems like there is an issue with the data transformation. <my-component :filter="stringobject" ></my-component> stringobject = "{"search_text":"(ciCl ...

The Angular modal service is failing to show up on the screen

I am having trouble implementing the angular modal service in my web application. When I click on the button, the modal does not appear. Can someone help me figure out what I am doing wrong? Builder View <div ng-controller="BuilderController as vm"> ...