Changeset 76e46d7 in uz for src/UploadZone.vue


Ignore:
Timestamp:
2022.10.08 18:53:06 (2 years ago)
Author:
js29a <js29a@…>
Branches:
master
Parents:
d45005e
Message:

to axios

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/UploadZone.vue

    rd45005e r76e46d7  
    33import { ref, computed, defineProps, withDefaults, defineEmits, getCurrentInstance, defineExpose, watch, nextTick } from 'vue'
    44
    5 import $ from 'jQuery'
     5import axios from 'axios'
    66
    77interface Props {
     
    5656}>()
    5757
     58let abort_controller: any = null
     59
    5860const do_emit = (code: string, ... args: any[]) => {
    5961  // @ts-ignore
     
    9395      for(const file of evt.dataTransfer.files)
    9496        files.push(file)
    95     else
     97    else // XXX elem click
    9698      for(const file of evt.target.files)
    9799        files.push(file)
     
    134136  if(state.value != State.s_wait)
    135137    return
     138
     139  try {
     140    abort_controller = new AbortController()
     141  }
     142  catch {
     143  }
    136144
    137145  do_emit('upload:start', [files])
     
    205213  cur_file += 1
    206214
    207   $.ajax({
     215  const options = {
     216    method: 'post',
    208217    url: props.target,
    209     type: 'POST',
    210218    data: fd,
    211     contentType: false,
    212     processData: false,
    213     xhr: (): any => {
    214       const xhr = new window.XMLHttpRequest()
    215 
    216       xhr.upload.addEventListener('progress', (evt): void => {
    217         current.value.forEach((item) => {
    218           if(item.file === cur) {
    219             item.current = evt.loaded
    220             item.total = evt.total
    221           }
    222         })
    223         do_emit('upload:progress', progress.value, current.value, queue.value)
     219    onUploadProgress: (evt: any) => {
     220      if(aborted && abort_controller)
     221        abort_controller.abort()
     222
     223      current.value.forEach((item) => {
     224        if(item.file === cur) {
     225          item.current = evt.loaded
     226          item.total = evt.total
     227        }
    224228      })
    225229
    226       return xhr
     230      do_emit('upload:progress', progress.value, current.value, queue.value)
    227231    }
    228   })
    229     .then((res: any) => {
     232  } as any
     233
     234  if(abort_controller)
     235    options.signal = abort_controller.signal
     236
     237  axios(options)
     238    .then((res) => {
    230239      current.value = current.value.filter((item) => {
    231240        return item.file !== cur
     
    234243      do_emit('upload:progress', progress.value, current.value, queue.value)
    235244
    236       progress.value.push(res)
    237       results.value.push(res)
     245      progress.value.push(res.data)
     246      results.value.push(res.data)
    238247
    239248      cur_jobs -= 1
     
    242251        if(props.autoReset || props.autoReset === '') {
    243252          state.value = State.s_idle
    244           files = []
     253          files = [] // # XXX can drop w/o reset
    245254          do_emit('files:clear', [])
    246255        }
     
    255264
    256265      if(cur_file == files.length && !cur_jobs) {
    257         files = []
     266        files = [] // XXX can drop w/o reset
    258267        do_emit('files:clear', [])
    259268
     
    279288        start_job()
    280289    })
    281     .catch((err: any) => {
     290    .catch((err) => {
     291      if(aborted && err.name == 'CanceledError') {
     292        state.value = State.s_aborted
     293        return err
     294      }
     295
    282296      if(props.keepGoing || props.keepGoing === '') {
    283297        current.value = current.value.filter((item) => {
     
    286300
    287301        errors.value.push({
    288           error: err,
     302          error: err.response,
    289303          file: cur
    290304        })
    291305
    292         do_emit('upload:error', err)
     306        do_emit('upload:error', err.response)
    293307
    294308        cur_jobs -= 1
     
    305319      else {
    306320        state.value = State.s_error
    307         error.value = err
    308         do_emit('upload:error', err)
     321        error.value = err.response
     322        do_emit('upload:error', err.response)
    309323      }
    310324    })
Note: See TracChangeset for help on using the changeset viewer.