eggjs

<form method="POST" action="http://localhost:7001/upload?_csrf={
   
   { ctx.csrf | safe }}" encType="multipart/form-data">
          title: <input name="title" />
  file: <input name="file" type="file" />
          <button type="submit">Upload</button>
        </form>
'use strict';
const path = require('path');
const sendToWormhole = require('stream-wormhole');
const fs = require('mz/fs');
const Controller = require('egg').Controller
class UploadController extends Controller {
  async index() {
    const { ctx } = this;
    // 获取 steam
    const stream = await ctx.getFileStream();
    // 生成文件名
    const filename = Date.now() + '' + Number.parseInt(Math.random() * 10000) + path.extname(stream.filename);
    // 写入路径
    const target = path.join(this.config.baseDir, 'app/public/upload/', filename);
    const writeStream = fs.createWriteStream(target);
    try {
      // 写入文件
      await stream.pipe(writeStream)
      // await stream.pipe(writeStream);
    } catch (err) {
      // 必须将上传的文件流消费掉,要不然浏览器响应会卡死
      await sendToWormhole(stream);
      throw err;
    }
    ctx.body = {
      a: stream.fields,
      b: `http://localhost:7001/public/upload/${filename}`
    };
  }
}


module.exports = UploadController;


      let studentIDs = JSON.stringify([userId]);
      let Row = {
        id: jobId,
        get_people: studentIDs,
        now_nums: 1
      }
      //直接插入string
      let isUpdate = await this.app.mysql.update('jobs', Row);
      const updateSuccess = isUpdate.affectedRows === 1;
      //检验插入是否成功
      if (!updateSuccess) {
        return {
          msg: '网络错误',
          status: -80
        }
      }

      //先修改jobs 再修改students
      //查询兼职表,找到对应id now_nums+1
      //
      let JobIDs = JSON.stringify([jobId]);
      //查询学生当前兼职任务
      let current_jobs = await this.app.mysql.query(`select current_job from student where id=${userId};`);
      //当前兼职任务为空,直接修改
      if (!current_jobs) {
        let row = {
          JobIDs,
          id: userId
        }
        let update_current_jobs = await this.app.mysql.update('student', row);
        const up_cu_jobs_su = update_current_jobs.affectedRows === 1;
        //修改students表
        if (!up_cu_jobs_su) {
          return {
            msg: '网络错误',
            status: -89
          }
        }
        if (up_cu_jobs_su) {
          return {
            msg: true,
            status: 89
          }
        }
      }
      //当前兼职任务不为空 查询兼职任务 转换出来,查找相同id 如果有return
      //没有相同id,修改当前任务
      if (current_jobs) {
        let existed_jobs = JSON.parse(current_jobs);

        const current_jobs_arr = await this.app.mysql.update('student', Row); // 更新 posts 表中的记录
        const updateSuccess = result.affectedRows === 1;
        if (updateSuccess) {
          await this.app.mysql.update('student',);
        }
        [].findIndex(function (value, index) {
          return value === 
        })
      }
      if (now_nums) {
        //组合插入

      }
      return result[0];

猜你喜欢

转载自blog.csdn.net/qq_45393395/article/details/120564672