Skip to content

question: <How to upload multi files of different fields> #662

Open
@winnnntttter

Description

@winnnntttter

I was trying to upload multi pictures with both the origin files and compressed blob named "picFile" and "thumbUrl"

let fd = new FormData();
for (let file of fileList.value) {
  fd.append("picFile", file.originFileObj); // file 
 /*  let thumbUrl = compress(file.thumbUrl)
  console.log(thumbUrl) */
  fd.append("thumbUrl", file.originFileObj); // blob ,changed to originFileObj is the same result
}
@Post("/upload")
async uploadPic(@Body({ required: true }) picInfo: any, @UploadedFiles("picFile") picFile: any, @UploadedFiles("thumbUrl") thumbUrl: any): Promise<any> {}
allowHeaders: ["Content-Type", "Authorization", "Accept","X-Requested-With","content-type", "authorization"]

The problem:
if I upload both "picFile" and "thumbUrl" got an CORS error "Access to XMLHttpRequest at 'http://localhost:3001/apis/upload?timestamp=1613804673403' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."
if I delete "picFile" or "thumbUrl" , I can upload success.

use multer option is the same result

// fileUploadOptions 
export default {
  storage: multer.diskStorage({
    destination: (req: any, file: any, cb: any) => {
      cb(null, "public/uploads/");
    },
    filename: (req: any, file: any, cb: any) => {
      const fileFormat = file.originalname.split(".");
      // cb(null, Date.now() + "." + fileFormat[fileFormat.length - 1]);
      cb(null, file.originalname);
    }
  }),
  fileFilter: (req: any, file: any, cb: any) => {
    if (!/image/.test(file.mimetype)) {
      return cb(null, false, new Error("I don't have a clue!"));
    }
    cb(null, true);
  },
  limits: {
    fieldNameSize: 255,
    fileSize: 1024 * 1024 * 5
  }
};
@Post("/upload")
async uploadPic(@Body({ required: true }) picInfo: any, @UploadedFiles("picFile", { options: fileUploadOptions }) picFile: any,@UploadedFiles("thumbUrl") thumbUrl: any): Promise<any> {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questionQuestions about the usage of the library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions