-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
173 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/d2-crud-plus-extends/src/file-uploder/lib/uploader/form/ajax.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
function getError (action, option, xhr) { | ||
let msg | ||
if (xhr.response) { | ||
msg = `${xhr.response.error || xhr.response}` | ||
} else if (xhr.responseText) { | ||
msg = `${xhr.responseText}` | ||
} else { | ||
msg = `fail to post ${action} ${xhr.status}` | ||
} | ||
|
||
const err = new Error(msg) | ||
err.status = xhr.status | ||
err.method = 'post' | ||
err.url = action | ||
return err | ||
} | ||
|
||
function getBody (xhr) { | ||
const text = xhr.responseText || xhr.response | ||
if (!text) { | ||
return text | ||
} | ||
|
||
try { | ||
return JSON.parse(text) | ||
} catch (e) { | ||
return text | ||
} | ||
} | ||
|
||
export default function upload (option, onSuccess, onError) { | ||
if (typeof XMLHttpRequest === 'undefined') { | ||
return | ||
} | ||
|
||
const xhr = new XMLHttpRequest() | ||
const action = option.action | ||
|
||
if (xhr.upload) { | ||
xhr.upload.onprogress = function progress (e) { | ||
if (e.total > 0) { | ||
e.percent = e.loaded / e.total * 100 | ||
} | ||
option.onProgress(e) | ||
} | ||
} | ||
|
||
const formData = new FormData() | ||
|
||
if (option.data) { | ||
Object.keys(option.data).forEach(key => { | ||
formData.append(key, option.data[key]) | ||
}) | ||
} | ||
|
||
formData.append(option.filename, option.file, option.file.name) | ||
|
||
xhr.onerror = function error (e) { | ||
// option.onError(e) | ||
onError(e) | ||
} | ||
|
||
xhr.onload = function onload () { | ||
if (xhr.status < 200 || xhr.status >= 300) { | ||
return option.onError(getError(action, option, xhr)) | ||
} | ||
|
||
// option.onSuccess(getBody(xhr)) | ||
onSuccess(getBody(xhr)) | ||
} | ||
|
||
xhr.open('post', action, true) | ||
|
||
if (option.withCredentials && 'withCredentials' in xhr) { | ||
xhr.withCredentials = true | ||
} | ||
|
||
const headers = option.headers || {} | ||
|
||
for (let item in headers) { | ||
if (headers.hasOwnProperty(item) && headers[item] !== null) { | ||
xhr.setRequestHeader(item, headers[item]) | ||
} | ||
} | ||
xhr.send(formData) | ||
return xhr | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/d2-crud-plus-extends/src/file-uploder/lib/uploader/form/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import config from '../../config' | ||
import ajax from './ajax' | ||
export default { | ||
beforeUpload (file, custom) { | ||
let fileName = file.name | ||
console.log('-----------开始上传----------', fileName) | ||
return new Promise(resolve => { | ||
resolve({}) | ||
}) | ||
}, | ||
doUpload (option, custom, context) { | ||
return new Promise((resolve, reject) => { | ||
console.log('-----', option) | ||
ajax(option, | ||
(res) => { | ||
resolve(config.form.successHandle(res)) | ||
}, | ||
(e) => { | ||
option.onError(e) | ||
reject(e) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters