Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]支持background的文件上传 #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion background.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ chrome.runtime.onMessage.addListener(function (request, _, cb) {
}
})


function dataURLtoFile(dataUrl, filename) {
var arr = dataUrl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, { type: mime });
}

function sendAjax(req, successFn, errorFn) {
var formDatas;
var xhr = new XMLHttpRequest();
Expand All @@ -219,7 +229,18 @@ function sendAjax(req, successFn, errorFn) {
if (!req.headers['Content-Type'] || req.headers['Content-Type'] == 'application/x-www-form-urlencoded') {
req.headers['Content-Type'] = 'application/x-www-form-urlencoded';
req.data = formUrlencode(req.data);
} else if (typeof req.data === 'object' && req.data) {
} else if (req.headers['Content-Type'] === 'multipart/form-data') {
delete req.headers['Content-Type'];
let formDatas = new FormData();
for (var item of req.formDatas) {
if (item.is_file) {
formDatas.append(item.name, dataURLtoFile(item.value, item.fileName))
}else {
formDatas.append(item.name, item.value);
}
}
req.data = formDatas;
} else if (typeof req.data === 'object' && req.data) {
req.data = JSON.stringify(req.data);
}
}else{
Expand Down
42 changes: 38 additions & 4 deletions response.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,44 @@ function sendAjaxByContent(req, successFn, errorFn) {
function sendAjaxByBack(id, req, successFn, errorFn) {
successFns[id] = successFn;
errorFns[id] = errorFn;
connect.postMessage({
id: id,
req: req
});
if (req.headers['Content-Type'] === 'multipart/form-data') {
var formDatas = []
if (req.data) {
for (var name in req.data) {
formDatas.push({name, value: req.data[name], is_file: false});
}
}
if (req.files) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果没有文件的话 可能会存在问题

let allPromise = [];
for (var name in req.files) {
let fileTransfer = new Promise(function (resolve, reject){
var files = document.getElementById(req.files[name]).files;
let file = files[0]
var reader = new FileReader();
reader.name = name;
reader.fileName = file.name;
reader.onload = function () {
resolve({name: this.name, value: this.result,is_file: true, fileName: this.fileName});
}
reader.readAsDataURL(file);
})
allPromise.push(fileTransfer);
}
Promise.all(allPromise).then(function(result){
formDatas = formDatas.concat(result);
req.formDatas = formDatas;
connect.postMessage({
id: id,
req: req,
});
})
}
} else {
connect.postMessage({
id: id,
req: req
});
}
}

connect.onMessage.addListener(function (msg) {
Expand Down