【egg】关于egg-oss上传的问题,用form表单上传没问题,使用ajax提交不上去
发布于 7 年前 作者 2VCl1md 5891 次浏览 来自 问答

请问怎么做中转,我先通过stream-wormhole上传文件

const stream = yield this.getFileStream();
	  let filepath = path.join(this.app.config.baseDir, `app/public/images/pcbFile/${stream.filename}`);
	  if (stream.fields.title === 'mock-error') {
	    filepath = path.join(this.app.config.baseDir, `app/public/images/not-exists/dir/${stream.filename}`);
	  } else if (stream.fields.title === 'mock-read-error') {
	    filepath = path.join(this.app.config.baseDir, `app/public/images/read-error-${stream.filename}`);
	  }
	  this.logger.warn('Saving %s to %s', stream.filename, filepath);
	  try {
	    yield saveStream(stream, filepath);
	  } catch (err) {
	    yield sendToWormhole(stream);
	    throw err;
	  }
 	console.log(stream)

然后想问一下怎么再用egg-oss把文件再次上传上去,或者有更好的方法 这是官方的使用示例

直接ajax使用这个上传到oss可以,但是是跨域的

const parts = this.multipart();
  let object;
  let part;
  part = yield parts;
  while (part) {
    if (part.length) {
      // arrays are busboy fields
      console.log('field: ' + part[0]);
      console.log('value: ' + part[1]);
      console.log('valueTruncated: ' + part[2]);
      console.log('fieldnameTruncated: ' + part[3]);
    } else {
      // otherwise, it's a stream
      console.log('field: ' + part.fieldname);
      console.log('filename: ' + part.filename);
      console.log('encoding: ' + part.encoding);
      console.log('mime: ' + part.mime);
      // file handle
      object = yield this.oss.put('egg-oss-demo-' + part.filename, part);
    }
    part = yield parts;
  }
  console.log('and we are done parsing the form!');
  if (object) {
    console.log('get oss object: %j', object);
    this.unsafeRedirect(object.url);
  } else {
    this.body = 'please select a file to upload!';
  }

请各位指教一下

1 回复

可以看看 https://github.com/eggjs/egg/issues/1363https://github.com/eggjs/egg-oss

这个 egg-oss 也是封装了 ali-oss, 所以要去看看 ali-oss 怎么弄

回到顶部