ajax文件上传

softwarePackFileChange() {
      const fileTypes = "exe,rar,zip";
      const fileTypeArray = fileTypes.split(",");

      const softwarePackFile = $("#softwarePackFile").get(0).files[0];
      const filePathArray = softwarePackFile.name.toLowerCase().split('.');
      const fileType = filePathArray[filePathArray.length - 1];
      if (_.indexOf(fileTypeArray, fileType) == -1) {
        $WarnMessage(`支持的附件格式【${fileTypes}】,当前格式:${fileType}。`);
        return;
      }

      const formData = new window.FormData();
      formData.append("file", softwarePackFile);
      formData.append("versionId", this.versionId);
      formData.append("shelfId", this.shelfId);

       $.ajax({
         type: "POST",
         data: formData,
         contentType: false,
         processData: false,
         url: `${$$apiDevOpsPath}/uploadAppPkg`,
         success: response => {
           const message = response.message;
           if (message != undefined) {
             $WarnMessage(message);
           } else {
             const shelfVersionAppPkg = response.model;
             shelfVersionAppPkg.minioHost = response.host;
             this.$emit("pushAppSoftwarePackage", shelfVersionAppPkg);
           }
         },
       });
   
  }
原文地址:https://www.cnblogs.com/150536FBB/p/13050791.html