Skip to content

Instantly share code, notes, and snippets.

@resting
Forked from ibreathebsb/upload.js
Last active August 15, 2018 07:54
Show Gist options
  • Select an option

  • Save resting/282ee1a45d5d3088f907878556f37ae5 to your computer and use it in GitHub Desktop.

Select an option

Save resting/282ee1a45d5d3088f907878556f37ae5 to your computer and use it in GitHub Desktop.

Revisions

  1. resting revised this gist Aug 15, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion upload.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ const dataURLtoFile = (dataurl, filename) => {
    let n = bstr.length
    const u8arr = new Uint8Array(n)
    while (n) {
    u8arr[n] = bstr.charCodeAt(n)
    u8arr[n-1] = bstr.charCodeAt(n-1)
    n -= 1 // to make eslint happy
    }
    return new File([u8arr], filename, { type: mime })
  2. @ibreathebsb ibreathebsb revised this gist Mar 15, 2018. No changes.
  3. @ibreathebsb ibreathebsb revised this gist Mar 15, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions upload.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // Note: only for modern browser
    import axios from 'axios'

    // helper function: generate a new file from base64 String
  4. @ibreathebsb ibreathebsb revised this gist Mar 15, 2018. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions upload.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import axios from 'axios'

    // helper function: generate a new file from base64 String
    // helper function: generate a new file from base64 String
    const dataURLtoFile = (dataurl, filename) => {
    const arr = dataurl.split(',')
    const mime = arr[0].match(/:(.*?);/)[1]
    @@ -17,13 +17,13 @@ const dataURLtoFile = (dataurl, filename) => {
    // generate file from base64 string
    const file = dataURLtoFile('data:image/png;base64,iVBORw0KGgoAAAANSUhEU...')
    // put file into form data
    const data=new FormData()
    data.append('img',file,file.name)
    const data = new FormData()
    data.append('img', file, file.name)

    // now upload
    const config = {
    headers:{'Content-Type': 'multipart/form-data'}
    };
    axios.post('/path/to/upload', data, config).then(response => {
    console.log(response.data)
    })
    // now upload
    const config = {
    headers: { 'Content-Type': 'multipart/form-data' }
    }
    axios.post('/path/to/upload', data, config).then(response => {
    console.log(response.data)
    })
  5. @ibreathebsb ibreathebsb revised this gist Mar 15, 2018. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions upload.js
    Original file line number Diff line number Diff line change
    @@ -17,13 +17,13 @@ const dataURLtoFile = (dataurl, filename) => {
    // generate file from base64 string
    const file = dataURLtoFile('data:image/png;base64,iVBORw0KGgoAAAANSUhEU...')
    // put file into form data
    const data=new FormData();
    data.append('img',file,file.name);
    const data=new FormData()
    data.append('img',file,file.name)

    // now upload
    const config = {
    headers:{'Content-Type':'multipart/form-data'}
    headers:{'Content-Type': 'multipart/form-data'}
    };
    axios.post('/path/to/upload',data,config).then(response)=>{
    console.log(response.data);
    axios.post('/path/to/upload', data, config).then(response => {
    console.log(response.data)
    })
  6. @ibreathebsb ibreathebsb revised this gist Mar 15, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion upload.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ const dataURLtoFile = (dataurl, filename) => {
    }

    // generate file from base64 string
    const f = dataURLtoFile('data:image/png;base64,iVBORw0KGgoAAAANSUhEU...')
    const file = dataURLtoFile('data:image/png;base64,iVBORw0KGgoAAAANSUhEU...')
    // put file into form data
    const data=new FormData();
    data.append('img',file,file.name);
  7. @ibreathebsb ibreathebsb created this gist Mar 15, 2018.
    29 changes: 29 additions & 0 deletions upload.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    import axios from 'axios'

    // helper function: generate a new file from base64 String
    const dataURLtoFile = (dataurl, filename) => {
    const arr = dataurl.split(',')
    const mime = arr[0].match(/:(.*?);/)[1]
    const bstr = atob(arr[1])
    let n = bstr.length
    const u8arr = new Uint8Array(n)
    while (n) {
    u8arr[n] = bstr.charCodeAt(n)
    n -= 1 // to make eslint happy
    }
    return new File([u8arr], filename, { type: mime })
    }

    // generate file from base64 string
    const f = dataURLtoFile('data:image/png;base64,iVBORw0KGgoAAAANSUhEU...')
    // put file into form data
    const data=new FormData();
    data.append('img',file,file.name);

    // now upload
    const config = {
    headers:{'Content-Type':'multipart/form-data'}
    };
    axios.post('/path/to/upload',data,config).then(response)=>{
    console.log(response.data);
    })