这是我在服务器中的接收器。
@Mutation(() => String)
async register(
@Arg("picture", () => GraphQLUpload) { createReadStream, filename }: any
): Promise<string> {
console.log(__dirname);
try {
const promise = await new Promise(async (resolve, reject) =>
createReadStream()
.pipe(createWriteStream(__dirname + `/../../../images/${filename}`))
.on("finish", () => resolve(true))
.on("error", () => reject(false))
);
} catch (err) {
console.log(err);
}
return "";
}
这是我的原始 register.graphql
文件,后来使用 graphql-codegen 编译。
mutation Register($picture: Upload!) {
register(picture:$picture)
}
我被困在有角的部分。到目前为止,我有这个。
uploadClicked($event: any) {
let file = $event.target.files[0];
console.log(file);
this.registerGQL.mutate({ picture: { file } }).subscribe((res) => {
console.log(res);
});
}