Skip to content

Instantly share code, notes, and snippets.

@azl397985856
Created September 1, 2020 07:55
Show Gist options
  • Save azl397985856/3f9f1f5327c53d8f48e5a1ec5c1b1d88 to your computer and use it in GitHub Desktop.
Save azl397985856/3f9f1f5327c53d8f48e5a1ec5c1b1d88 to your computer and use it in GitHub Desktop.
// multi-part
// client.mjs
import fs from "fs";
import path, { dirname } from "path";
import request from "request";
import { fileURLToPath } from "url";
var r = request.post("http://localhost:7001/low-code/api/v1/magic", function(
err,
response,
body
) {
if (err) throw err;
if (response.statusCode !== 200) throw response;
console.log("upload success", body);
});
var form = r.form();
form.append(
"fmJs",
fs.createReadStream(
path.join(
dirname(fileURLToPath(import.meta.url)),
"../dist/FormMaking.umd.min.js"
)
)
);
form.append(
"fmCss",
fs.createReadStream(
path.join(dirname(fileURLToPath(import.meta.url)), "../dist/FormMaking.css")
)
);
// server.mjs
async uploadFormMaking() {
const { ctx } = this;
const parts = ctx.multipart();
let part;
let js;
let css;
while ((part = await parts()) != null) {
if (part.fieldname === "fmCss") {
css = await ctx.service.oss.put("low-code/form-making.css", part, {
headers: { "x-oss-forbid-overwrite": true },
});
} else if (part.fieldname === "fmJs") {
js = await ctx.service.oss.put("low-code/form-making.js", part, {
headers: { "x-oss-forbid-overwrite": true },
});
} else {
ctx.body = ctx.helper.fail("不支持的字段");
}
ctx.body = ctx.helper.success({
js,
css,
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment