【改进】固件上传

This commit is contained in:
2025-11-12 09:26:16 +08:00
parent 746e5051cf
commit 57f97a490a
4 changed files with 77 additions and 11 deletions

View File

@@ -93,8 +93,8 @@ public class BizFirmwareController {
*/
@PostMapping("/uploadFile")
@ResponseBody
public Result<String> uploadFile(MultipartFile file) {
return Result.success();
public Result<String> uploadFile(MultipartFile file, Long id) {
return Result.isBool(firmwareService.uploadFile(file, id));
}
}

View File

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.corewing.app.common.Result;
import com.corewing.app.entity.Firmware;
import org.springframework.web.multipart.MultipartFile;
/**
* 固件 Service 接口
@@ -24,4 +25,12 @@ public interface FirmwareService extends IService<Firmware> {
* @return 固件信息
*/
Firmware getByFirmwareName(String firmwareName);
/**
* 上传文件
* @param file
* @param id
* @return
*/
boolean uploadFile(MultipartFile file, Long id);
}

View File

@@ -7,8 +7,12 @@ import com.corewing.app.common.page.PageContext;
import com.corewing.app.entity.Firmware;
import com.corewing.app.mapper.FirmwareMapper;
import com.corewing.app.service.FirmwareService;
import com.corewing.app.util.OSSUploadUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
* 固件 Service 实现类
@@ -32,5 +36,18 @@ public class FirmwareServiceImpl extends ServiceImpl<FirmwareMapper, Firmware> i
return this.getOne(wrapper);
}
@Override
public boolean uploadFile(MultipartFile file, Long id) {
try {
String downloadUrl = OSSUploadUtil.uploadFile(file.getInputStream(), "/");
Firmware firmware = getById(id);
firmware.setDownloadUrl(downloadUrl);
updateById(firmware);
} catch (IOException e) {
throw new RuntimeException(e);
}
return false;
}
}

View File

@@ -219,6 +219,7 @@
id="firmwareFile"
name="file"
accept=".bin"
ref="firmwareFileRef"
>
<div class="form-text text-muted mt-1">支持 .bin 格式,单个文件不超过 100MB</div>
</div>
@@ -499,20 +500,59 @@
},
// 打开文件上传模态框
openUploadModal(item) {
this.addOrEditDto = item;
this.modalInstances['uploadFileModal'].show();
},
uploadFile() {
request.put("/biz/user/resetPasswordRequest", this.resetPasswordDto)
.then((res) => {
if (res.code === 200) {
$message.success('修改成功!', 500);
this.modalInstances['resetPwdModal'].hide();
} else {
alert('修改失败!');
async uploadFile() {
// 1. 获取选中的文件
const fileInput = this.$refs.firmwareFileRef;
const file = fileInput.files[0];
// 2. 校验文件(格式、大小)
if (!file) {
alert("请选择 .bin 格式的固件文件");
return;
}
if (file.type !== "application/octet-stream" && !file.name.endsWith(".bin")) {
alert("仅支持 .bin 格式文件");
return;
}
if (file.size > 100 * 1024 * 1024) { // 100MB
alert("文件大小不能超过 100MB");
return;
}
const formData = new FormData();
formData.append("file", file);
formData.append("id", this.addOrEditDto.id);
// 4. 发送上传请求Axios
try {
const response = await axios({
url: "/biz/firmware/uploadFile",
method: "POST",
data: formData,
headers: {
"Content-Type": "multipart/form-data"
},
onUploadProgress: (progressEvent) => {
const progress = (progressEvent.loaded / progressEvent.total) * 100;
console.log(`上传进度:${progress.toFixed(2)}%`);
}
this.resetPasswordDto = {};
});
if (response.data.success) {
$message.success('固件上传成功');
this.$refs.firmwareFileRef.value = "";
this.modalInstances['uploadFileModal'].hide();
} else {
$message.error("上传失败:" + response.data.message);
}
} catch (error) {
console.error("上传出错:", error);
$message.error("网络异常,上传失败");
}
},
// 打开新增模态框
openAddModal() {