大家下午好,我正在尝试通过我自己的项目本地目录中的核心应用程序上传头像,并通过我的 .env 文件中的备用路由生成一个 URL,以便我可以在前端呈现,但是当试图访问由我的失眠生成的路线,他只是无法呈现下面的图像。
我的控制器的上传/主要
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Application from '@ioc:Adonis/Core/Application'
export default class UploadsController {
public async show({ params, response }: HttpContextContract) {
return response.download(Application.tmpPath('uploads', params.file))
}
}
型号/文件
import { BaseModel, column, computed } from '@ioc:Adonis/Lucid/Orm'
import { FileCategory } from 'App/Utils'
import Env from '@ioc:Adonis/Core/Env'
export default class File extends BaseModel {
@column({ isPrimary: true })
public id: number
@column({ serializeAs: null })
public fileCategory: FileCategory
@column()
public fileName: string
@column({ serializeAs: null })
public ownerId: number
@computed()
public get url(): string {
return `${Env.get('APP_URL')}/uploads/${this.fileName}`
}
}
路线/上传.ts
import Route from '@ioc:Adonis/Core/Route'
Route.get('/uploads/:file', 'Uploads/Main.show')
.env 和 env.ts 生成 URL
PORT=3333
HOST=0.0.0.0
NODE_ENV=development
APP_KEY=2WiUDyQD**
APP_URL=http://${HOST}:${PORT} //url generate
DB_CONNECTION=mysql
MYSQL_HOST=localhost
MYSQL_PORT=3307
MYSQL_USER=root
MYSQL_PASSWORD=se**
MYSQL_DB_NAME=**
CACHE_VIEWS=false
SMTP_HOST=s**
SMTP_PORT=2**
SMTP_USERNAME=133c97e**
SMTP_PASSWORD=c520ea**
环境.ts
import Env from '@ioc:Adonis/Core/Env'
export default Env.rules({
HOST: Env.schema.string({ format: 'host' }),
PORT: Env.schema.number(),
APP_KEY: Env.schema.string(),
APP_NAME: Env.schema.string(),
APP_URL: Env.schema.string(), //url
NODE_ENV: Env.schema.enum(['development', 'production', 'testing'] as const),
DB_CONNECTION: Env.schema.string(),
MYSQL_HOST: Env.schema.string({ format: 'host' }),
MYSQL_PORT: Env.schema.number(),
MYSQL_USER: Env.schema.string(),
MYSQL_PASSWORD: Env.schema.string.optional(),
MYSQL_DB_NAME: Env.schema.string(),
SMTP_HOST: Env.schema.string({ format: 'host' }),
SMTP_PORT: Env.schema.number(),
SMTP_USERNAME: Env.schema.string(),
SMTP_PASSWORD: Env.schema.string()
})