'NestJS upload multiple files using GraphQL
I tried to upload multiple files using Nestjs Graphql but i can't ..
this code works fine with controller (Rest) exactly in https://docs.nestjs.com/techniques/file-upload
@Post('upload')
@UseInterceptors(FilesInterceptor('files',saveImageToStorage))
uploadImages(@UploadedFiles() files:Array<Express.Multer.File>, @Req() req:Request):any{
return ;
}
but it can't work with Graphql may be because (FileInterceptor) can not work with graphql , how i impelment this code or this way with graphql to upload multiple images?
note: I tried many ways to upload multiple images using nest graph(with multer and with graphql-upload) but all ways failed !
image-storage.ts
import { diskStorage } from "multer";
import { generate } from 'shortid';
import * as fs from 'fs';
//const FileType = require('file-type');
import path = require('path');
type validFileExtension = 'png' | 'jpg' | 'jpeg';
type validMimeType = 'image/png' | 'image/jpg' | 'image/jpeg';
const validFileExtensions: validFileExtension[] = ['png' , 'jpg' , 'jpeg'];
const validMimeTypes: validMimeType[] = ['image/png' , 'image/jpg' , 'image/jpeg'];
export const saveImageToStorage = {
storage:diskStorage({
destination:'./images',
filename:(req,file,cb)=>{
const fileExtension:string = path.extname(file.originalname);
const fileName:string = generate() + fileExtension
cb(null,fileName)
}
}),
fileFilter: (req, file, cb) => {
const allowedMimeTypes: validMimeType[] = validMimeTypes;
allowedMimeTypes.includes(file.mimetype) ? cb(null,true) : cb(null,false)
}
}
export const removeFile = (fullFilePath:string):void => {
try{
fs.unlinkSync(fullFilePath);
} catch(err) {
console.log(err);
}
}
Solution 1:[1]
Could you please provide the AppModule code ?
Have you tried this :
import { graphqlUploadExpress } from 'graphql-upload';
...
....
....
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(graphqlUploadExpress()).forRoutes('graphql');
}
}
and in your GraphQLModule parameters :
uploads: false
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sydwell |
