为什么req.files是file.size 0?

时间:2019-02-26 10:55:30

标签: node.js express react-native multipartform-data expo

我用数组存储了几个文件。

const router = express.Router();
const multer = require("multer"); // multer모듈 적용 (for 파일업로드)
const storage = multer.diskStorage({
  // storage 이미지를 해쉬가 아니라 그대로 저장하기위해서 씀
  destination: function(req, file, cb) {
    cb(null, "images/"); // cb 콜백함수를 통해 전송된 파일 저장 디렉토리 설정
  },
  filename: function(req, file, cb) {
    //   cb(null, file.originalname); // cb 콜백함수를 통해 전송된 파일 이름 설정
    cb(null, new Date().toISOString() + file.originalname);
  }
});
var upload = multer({ storage: storage, limits: { fileSize: 2000000 } });
.
.
.

router.post("/upload", upload.array("userfile", 3), function(req, res) {
  var username = req.body.username;
  var addresstext = req.body.addresstext;
  var introduce = req.body.introduce;
  var url = "http://13.209.69.225:3000/";
  console.log(username);
  console.log(addresstext);
  console.log(introduce);

  res.send({
    message: "Uploaded! : " + JSON.stringify(req.files)
  }); 
  console.log(req.files);

尽管它最多可以存储三个文件,但是如果将两个文件发送到服务器,则一个文件的大小为零。

CMD屏幕

[ { fieldname: 'userfile',
    originalname: 'photo.jpg',
    encoding: '7bit',
    mimetype: 'image/jpeg',
    destination: 'images/',
    filename: '2019-02-26T10:49:47.277Zphoto.jpg',
    path: 'images/2019-02-26T10:49:47.277Zphoto.jpg',
    size: 1816426 },
  { fieldname: 'userfile',
    originalname: 'photo2.jpg',
    encoding: '7bit',
    mimetype: 'image/jpg',
    destination: 'images/',
    filename: '2019-02-26T10:49:47.486Zphoto2.jpg',
    path: 'images/2019-02-26T10:49:47.486Zphoto2.jpg',
    size: 0 } ]

即使保存了文件名,由于没有文件大小,打开文件时也为空。

Client.js

fileSend = () => {
    const apiUrl = "http://13.209.69.225:3000/upload";
    const uri = this.state.image;
    const uri2 = this.state.image2;
    const stringdata = {
      username: this.state.sittername,
      introduce: this.state.introducetext,
      addresstext: this.state.addresstext
    };
    const uriParts = uri.split(".");
    const fileType = uriParts[uriParts.length - 1];
    const formData = new FormData();

    formData.append("userfile", {
      uri,
      name: `photo.${fileType}`,
      type: `image/${fileType}`
    });
    formData.append("userfile", {
      uri2,
      name: `photo2.${fileType}`,
      type: `image/${fileType}`
    });
    for (var k in stringdata) {
      formData.append(k, stringdata[k]);
    }

    const options = {
      method: "POST",
      body: formData,

      headers: {
        Accept: "application/json",
        "Content-Type": "multipart/form-data"
        // "Content-Type": "application/json"
      }
    };
    // console.log("test" + this.state.image2);
    //  console.log("image" + this.state.image);
    console.log("eeeeee" + fileType);
    //  console.log("gggggg" + JSON.stringify(options));
    return fetch(apiUrl, options)
      .then(response => response.json())
      .then(res => {
        alert(res.message);
      })
      .catch(err => {
        console.log(err);
      });
  };

客户端重新调用文件时,它还会检查控制台是否存在文件数据。

这是服务器设置问题吗?我认为这不是位置问题。我不知道。 我需要你的帮助。

0 个答案:

没有答案