1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vue.jsでfile postする例

Posted at

vue.jsでfile postする例です。
何かのご参考になれば。

template#file_new
  = file_field_tag :files, multiple: true, 'v-on:change' => 'fileInputChange'
  = submit_tag 'Upload', 'v-on:click.prevent' => 'fileUpload'
$ ->
  Vue.component 'file-new',
    template: '#file_new'
    data: ->
      file_input: undefined
      files: undefined
    methods:
      fileInputChange: (e) ->
        @file_input = e.target
        @files = @file_input.files
      fileUpload: ->
        return if @files.length <= 0
        $.each Array.prototype.slice.call(@files, 0), (i, file) =>
          @_handleUpload(file)
        @file_input.value = ''
      _handleUpload: (file) ->
        form = new FormData()
        form.append('Content-Type', file.type || 'application/octet-stream')
        form.append('file', file)

        $.ajax
          url: "/api/hoge.json"
          type: 'POST'
          data: form
          cache: false
          contentType: false
          processData: false
        .done (response) =>
          console.log response
        .fail (response) =>
          console.error response
1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?