【改进】固件上传
This commit is contained in:
@@ -93,8 +93,8 @@ public class BizFirmwareController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("/uploadFile")
|
@PostMapping("/uploadFile")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public Result<String> uploadFile(MultipartFile file) {
|
public Result<String> uploadFile(MultipartFile file, Long id) {
|
||||||
return Result.success();
|
return Result.isBool(firmwareService.uploadFile(file, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.corewing.app.common.Result;
|
import com.corewing.app.common.Result;
|
||||||
import com.corewing.app.entity.Firmware;
|
import com.corewing.app.entity.Firmware;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 固件 Service 接口
|
* 固件 Service 接口
|
||||||
@@ -24,4 +25,12 @@ public interface FirmwareService extends IService<Firmware> {
|
|||||||
* @return 固件信息
|
* @return 固件信息
|
||||||
*/
|
*/
|
||||||
Firmware getByFirmwareName(String firmwareName);
|
Firmware getByFirmwareName(String firmwareName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
* @param file
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
boolean uploadFile(MultipartFile file, Long id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,12 @@ import com.corewing.app.common.page.PageContext;
|
|||||||
import com.corewing.app.entity.Firmware;
|
import com.corewing.app.entity.Firmware;
|
||||||
import com.corewing.app.mapper.FirmwareMapper;
|
import com.corewing.app.mapper.FirmwareMapper;
|
||||||
import com.corewing.app.service.FirmwareService;
|
import com.corewing.app.service.FirmwareService;
|
||||||
|
import com.corewing.app.util.OSSUploadUtil;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 固件 Service 实现类
|
* 固件 Service 实现类
|
||||||
@@ -32,5 +36,18 @@ public class FirmwareServiceImpl extends ServiceImpl<FirmwareMapper, Firmware> i
|
|||||||
return this.getOne(wrapper);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -219,6 +219,7 @@
|
|||||||
id="firmwareFile"
|
id="firmwareFile"
|
||||||
name="file"
|
name="file"
|
||||||
accept=".bin"
|
accept=".bin"
|
||||||
|
ref="firmwareFileRef"
|
||||||
>
|
>
|
||||||
<div class="form-text text-muted mt-1">支持 .bin 格式,单个文件不超过 100MB</div>
|
<div class="form-text text-muted mt-1">支持 .bin 格式,单个文件不超过 100MB</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -499,20 +500,59 @@
|
|||||||
},
|
},
|
||||||
// 打开文件上传模态框
|
// 打开文件上传模态框
|
||||||
openUploadModal(item) {
|
openUploadModal(item) {
|
||||||
|
this.addOrEditDto = item;
|
||||||
this.modalInstances['uploadFileModal'].show();
|
this.modalInstances['uploadFileModal'].show();
|
||||||
},
|
},
|
||||||
uploadFile() {
|
async uploadFile() {
|
||||||
request.put("/biz/user/resetPasswordRequest", this.resetPasswordDto)
|
// 1. 获取选中的文件
|
||||||
.then((res) => {
|
const fileInput = this.$refs.firmwareFileRef;
|
||||||
if (res.code === 200) {
|
const file = fileInput.files[0];
|
||||||
$message.success('修改成功!', 500);
|
|
||||||
this.modalInstances['resetPwdModal'].hide();
|
// 2. 校验文件(格式、大小)
|
||||||
} else {
|
if (!file) {
|
||||||
alert('修改失败!');
|
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() {
|
openAddModal() {
|
||||||
|
|||||||
Reference in New Issue
Block a user