Vue file upload - uploading the identical file multiple times in a row

Currently, I am working on implementing a file upload feature for a chat application without using forms. Here is a snippet of my HTML:

<span class="file-attach mr3">
  <input id="uploadFile" name="uploadFile" type="file" @change="attachFile($event.target.files)" multiple>
  <i class="w-10 ml4 icon-attach"></i>
</span>

In addition, here is how my TypeScript file is structured:

 async attachFile(files) {
if (!files.length) {
  return
}
const reader = new FileReader()
reader.addEventListener('loadend', () => {
  this.mediaLinkDocument = {
    title: this.file.type.indexOf('image') !== -1 ? '' : this.file.name,
    type: this.file.type,
    size: this.file.size,
  }
  this.sendFile(this.file)
})

Array.from(Array(files.length).keys()).forEach((id) => {
  console.log(files[id])
  this.file = files[id]

  if (this.file.size > MAX_ATTACHMENT_SIZE) {
    alert('O tamanho máximo é de 20 MB')
    this.file = undefined

    return
  }
  reader.readAsDataURL(this.file)
  files = {}
})

}

Although the file upload functionality works, there is an issue where if a user attempts to upload the same image again, the @change callback is not triggered and the file is not uploaded. Any suggestions or help would be greatly appreciated.

Answer №1

After conducting some investigation (refer to the comments), both myself and the question's author discovered an interesting difference between Chrome and Firefox. It appears that Chrome does not trigger a change event when the same file is selected twice, while Firefox does. To address this issue, I proposed a solution for him to reset the field value to an empty string after uploading a file. This way, reselecting the same file will register as a change.

Answer №2

When facing an issue where Google Chrome fails to trigger the change event on a file, I found a solution by implementing the following in my Vue.js code:

<input
   ref="ExcelfileUpload"
   type="file"
   multiple="false"
   id="sheets"
   @change="onchangeOpenOrderUpload"
/> 

onchangeOpenOrderUpload(e) {
    ... additional codes here
    this.$refs.ExcelfileUpload.value = "";
}

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

Click on an element in Angular to toggle it

A function in my codebase is responsible for toggling a specific section within a div element. Currently, there are a total of 3 sections available in the app. The functionality is implemented in the app.component.ts file: show = false; toggle() { ...

Strategies for effectively passing parameters to animation step functions

By breaking down an animation function into reusable parts, I created the following structure: const enterLeaveAnimation = ( triggerName: string, step1: AnimationMetadata | AnimationMetadata[], step2: AnimationMetadata | AnimationMetadata[] ) => t ...

Exploring the possibilities of Ionic 2 with Web Audio API

I am encountering issues while using the Web Audio API with Ionic 2. Despite my efforts, I keep running into errors. It seems that the problem lies within the TypeScript compiler. I attempted to resolve it by adding "es2015.promise", but to no avail. The e ...

In Production mode, Angular automatically reloads the page every time my data request service is executed to fetch data from the API

The Issue at Hand I have developed an angular application that functions flawlessly on the development server. This application utilizes localStorage to store the user's JWT token, although I am aware that this may not be the most secure method. How ...

Selecting the appropriate color code based on a specified value

If the value of zoneTempDiff1 falls below 1.5, consider using temp: 1 color. If it exceeds 1.5, opt for temp: 2 color. The same logic applies to all different values such as -1, -2, 0, 1, 2, 3, 4, or 5, each corresponding to a specific color code. In cas ...

Guide to creating varying component sizes using ReactJS and Styled Components

Is it possible to add variation to my button based on the prop 'size' being set to either 'small' or 'medium'? interface Props { size?: 'medium' | 'small'; } How can I adjust the size of the component us ...

Creating a truly dynamic component in Vue/Nuxt: A step-by-step guide

To implement a truly dynamic single page component, we can utilize the component tag like this: <component v-bind:is="componentName" :prop="someProperty"/> ... import DynamicComponent from '@/components/DynamicComponent.vue'; ... compon ...

Pair of elements connecting in Vuejs

What is the best way to efficiently organize data and communication between two Vue.js components? Here's an example scenario: 1) I have a component item(v-for="item in items) a {{item.name}} 2) And then the second component card(v-for="item in it ...

What is the best method for showcasing images from s3 on browser with vue/nuxt?

On my vue/nuxt app, I am struggling to display a user's profile picture uploaded to S3 through a file API. The main issue I am facing is not being able to convert the image to the correct format. Here are the problems I encountered: The file content ...

Adding input fields dynamically

I have a component named home with 3 input fields for Country, City, and State. You can see them in the image below: https://i.sstatic.net/ZMXfD.png I've implemented dynamic addition of input fields, and it's functioning well as shown in the im ...

Having trouble with the error "vue-cli-service ENOENT not found" after updating Vue CLI?

Yesterday, I encountered errors in my Vue project. Initially, I thought it was a node-related issue as my project was not loading properly, which was unusual. Additionally, I kept receiving an audit problem related to my CLI plugin. After updating Node to ...

A guide to iterating over an array and displaying individual elements in Vue

In my application, there is a small form where users can add a date with multiple start and end times which are then stored in an array. This process can be repeated as many times as needed. Here is how the array structure looks: datesFinal: {meetingName: ...

Tips for creating a TypeScript generic that ensures no SubType property is overly restricted

I am looking to create a function that can generically return a partial object of a specific type, where the object has an ID property. When working with a concrete type, such as the example below, the function works smoothly: type Person = { id: string; ...

Learn how to retrieve images from the web API at 'https://jsonplaceholder.typicode.com/photos' and showcase them on a webpage using Angular10

Using the API "https://jsonplaceholder.typicode.com/photos", I have access to 5 properties: albumId: 1 id: 1 thumbnailUrl: "https://via.placeholder.com/150/92c952" title: "accusamus beatae ad facilis cum similique qui sunt" url: "https://via.placeh ...

An error has occurred in Angular: No routes were found that match the URL segment 'null'

I've recently developed a simple Angular page that extracts an ID (a guid) from the URL and uses it to make an API call. While I have successfully implemented similar pages in the past without any issues, this particular one is presenting challenges w ...

Tips for bypassing the 'server-only' restrictions when executing commands from the command line

I have a NextJS application with a specific library that I want to ensure is only imported on the server side and not on the client side. To achieve this, I use import 'server-only'. However, I also need to use this file for a local script. The i ...

Unable to render Material Icons on Vue/Quasar web app hosted on Azure

I am facing an issue with my webapp built using Vue and Quasar. I added the necessary icon imports to my quasar-user-options.js file. import '@quasar/extras/material-icons/material-icons.css' import "@quasar/extras/material-icons-outlined/ma ...

How can I transfer the data from a file to upload it in Angular 9 without manually typing it out?

In my Angular application, I have a functionality where users can upload two files for processing on the server. However, I am looking to add a feature that allows users to simply copy and paste the contents of the files into two textboxes instead of going ...

What steps can be taken to eliminate redundancy in this code and improve its efficiency?

Here I have two methods, create and update, that send data to an API. I am looking to enhance the createUser and updateUser methods as they are very similar. Additionally, if you have any suggestions on a better way to directly set the id property as null ...

Troubleshooting: Ionic Cordova's Post Functionality Fails to

I am a newcomer to Hybrid application development, currently utilizing Ionic Cordova for my project. My aim is to implement a POST method within the application. var url = "http://XXXXXXXXXXXXX.com/XXXX"; var headers = new Headers(); headers.append("Acce ...