I’m trying to send some data from app to backend (some text, image and PDF file) but get only text. But if im trying to send this data from Postman to backend, i properly get all data.
Here is my NestJS code
file.entety.ts
import { Entity, Column, ObjectIdColumn, ObjectId } from 'typeorm';
@Entity()
export class File {
@ObjectIdColumn()
_id: ObjectId;
@Column()
destination: string;
@Column()
mimetype: string;
@Column()
originalname: string;
@Column()
encoding: string;
@Column()
path: string;
@Column()
fieldname: string;
@Column()
size: number;
@Column()
filename: string;
}
files.controller.spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { FilesController } from './files.controller';
import { FilesService } from './files.service';
describe('FilesController', () => {
let controller: FilesController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [FilesController],
providers: [FilesService],
}).compile();
controller = module.get<FilesController>(FilesController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
files.controller.ts
import {
Controller,
Get,
Post,
UseInterceptors,
UploadedFile,
} from '@nestjs/common';
import { FilesService } from './files.service';
import { FileInterceptor } from '@nestjs/platform-express';
import { Express } from 'express';
import { diskStorage } from 'multer';
import { editFileName } from './files.utils';
@Controller('files')
export class FilesController {
constructor(private readonly filesService: FilesService) {}
@Post()
@UseInterceptors(
FileInterceptor('file', {
storage: diskStorage({
destination: './uploads/',
filename: editFileName,
}),
}),
)
create(@UploadedFile() file: Express.Multer.File) {
return this.filesService.create(file);
}
@Get()
findAll() {
return this.filesService.findAll();
}
}
files.modules.ts
import { Module } from '@nestjs/common';
import { FilesService } from './files.service';
import { FilesController } from './files.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { File } from './entities/file.entity';
@Module({
imports: [TypeOrmModule.forFeature([File])],
controllers: [FilesController],
providers: [FilesService],
})
export class FilesModule {}
files.service.spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { FilesService } from './files.service';
describe('FilesService', () => {
let service: FilesService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [FilesService],
}).compile();
service = module.get<FilesService>(FilesService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
files.service.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { File } from './entities/file.entity';
@Injectable()
export class FilesService {
constructor(
@InjectRepository(File)
private fileRepository: Repository<File>,
) {}
async create(file: Express.Multer.File) {
const savedFile = await this.fileRepository.save(file);
const link = `http://www.example.com/uploads/${savedFile.filename}`;
return { link };
}
async findAll() {
const files = await this.fileRepository.find({
select: ['filename'],
});
return files.map(file => ({
link: `http://localhost:3001/news/${file.filename}`
}));
}
}
files.utils.ts
import { extname } from 'path';
export const editFileName = (req, file, callback) => {
const name = file.originalname.split('.')[0];
const fileExtName = extname(file.originalname);
const randomName = Array(4)
.fill(null)
.map(() => Math.round(Math.random() * 16).toString(16))
.join('');
callback(null, `${name}-${randomName}${fileExtName}`);
};