img[]
としなくてもimg
でも動作する
Last active
August 4, 2017 11:00
-
-
Save uupaa/9f8420b7e2df23c646ef70c17906634c to your computer and use it in GitHub Desktop.
image multi post
This file contains hidden or 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
export function postLocalImage(params, // @arg Object - { url: url, blob: Blob } | |
readyCallback, // @arg Function - readyCallback():void | |
errorCallback) { // @arg Function - errorCallback(error):void | |
let url = `/sd/upload/images`; | |
let form = new FormData(); | |
/* | |
form.append("img[]", params.blob, "000001.jpg"); // TBD: | |
form.append("img[]", params.blob, "000002.jpg"); // TBD: | |
form.append("img[]", params.blob, "000003.jpg"); // TBD: | |
*/ | |
form.append("img", params.blob, "000001.jpg"); // TBD: | |
form.append("img", params.blob, "000002.jpg"); // TBD: | |
form.append("img", params.blob, "000003.jpg"); // TBD: | |
const FETCH_POST_IMAGE_OPTIONS = { | |
method: "POST", | |
cache: "no-cache", | |
body: form, | |
headers: { | |
//"Content-Type": undefined | |
}, | |
}; | |
fetch(url, FETCH_POST_IMAGE_OPTIONS).then(resp => { | |
if (resp.ok) { | |
resp.json().then(json => { | |
console.log(json); | |
readyCallback(json) | |
}).catch(error => { | |
console.error(error); | |
}); | |
} else { | |
errorCallback(new Error(resp.statusText || resp.status)); | |
} | |
}).catch(err => { // 401 | |
errorCallback(err); | |
}); | |
} |
This file contains hidden or 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
const fs = require("fs"); | |
const express = require("express"); | |
const bodyParser = require("body-parser"); | |
const multer = require("multer"); // npm i -D multer | |
const os = require("os"); | |
const upload = multer({ dest: os.tmpdir() }); | |
app.post("/upload/images", upload.array("img", 2000), (req, res) => { // max 2000 items | |
console.log('file info: ', req.files); | |
let response_filenames = []; | |
let _sendResult = (statusCode) => { | |
if (statusCode === 200) { | |
res.json({ | |
message: 'File uploaded successfully', | |
filename: response_filenames | |
}); | |
} else { | |
res.send(500); | |
} | |
_sendResult = null; | |
}; | |
req.files.forEach(file => { | |
console.log({ | |
fieldname: file.fieldname, // "img" <- <input type="file" name="img"> | |
mimetype: file.mimetype, // "image/jpeg" | |
originalname: file.originalname, // '000001.jpg', | |
mimetype: file.mimetype, // 'image/jpeg', | |
destination: file.destination, // '/var/folders/vl/pzwvsw6s6jl_s2gtydc1srq5ffvzlg/T/', | |
filename: file.filename, // '440231f6991ce1128ddafe9c7c378472', | |
path: file.path, // '/var/folders/vl/pzwvsw6s6jl_s2gtydc1srq5ffvzlg/T/0d84d8cd518e97c6a9f43487aa27b005', | |
size: file.size // 553840 | |
}); | |
let target_path = "./upload_images/" + file.originalname; | |
fs.rename(file.path, target_path, function(err) { | |
if (err) { | |
console.log(err); | |
_sendResult(500); | |
} else { | |
response_filenames.filename.push(file.originalname); | |
if (response_filenames.filename.length >= req.files.length) { | |
if (_sendResult) { | |
_sendResult(200); | |
} | |
} | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment