开发笔记:前端 blob 浏览器内存二进制文件对象 文件上传(vue+springboot)

前端 blob 浏览器内存二进制 文件对象

blob:http://localhost:8888/575e20ab-7ddc-49d8-8365-ef3afcaa307b

前端代码

function blobToFile(blob, fileName) {
  return new File([blob], fileName, { type: blob.type });
}
const scanPhoto = async (brand:string,category:string,imageUrl:string) => {
  const response = await fetch(imageUrl);
  const blob = await response.blob();
  const file = blobToFile(blob, 'tagImage.jpg');
  let formData = new FormData()
  formData.append("file", file)
  formData.append('brand', brand)
  formData.append('category', category)
  let standard: any = await scanTagApi(formData)
  console.log("standard", standard)

}



function blobToFile(blob:any, fileName:string) {
  return new File([blob], fileName, { type: blob.type });
}

scanTagApi

export const scanTagApi = (data:any) => {
    return http({
        method: 'POST',
        url: '/scan/scanTag',
        headers: {
            'Content-Type': 'multipart/form-data',
        },
        data
    })
}

后端代码

    @PostMapping("/scanTag")
    public CommonResult<ScanDto> scanTag(@RequestParam("file")MultipartFile file, String brand, String category) {
        return CommonResult.success(this.scanService.scanTag(file,brand,category));
    }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以使用JPA或MyBatis等ORM框架来操作MySQL数据库。对于存储的二进制文件,可以使用byte[]类型来存储,然后在需要还原的地方将其从数据库中读取出来。 下面是一个示例: 1. 创建一个实体类来映射数据库中的表: ```java @Entity @Table(name = "file") public class FileEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String fileName; private byte[] content; // getter and setter } ``` 2. 在Service层中定义一个方法来获取二进制文件: ```java @Service public class FileService { @Autowired private FileRepository fileRepository; public byte[] getFileContent(Long fileId) { return fileRepository.findById(fileId) .orElseThrow(() -> new RuntimeException("File not found")) .getContent(); } } ``` 3. 在Controller层中定义一个接口来返回二进制文件: ```java @RestController public class FileController { @Autowired private FileService fileService; @GetMapping("/file/{id}") public ResponseEntity<byte[]> getFile(@PathVariable Long id) { byte[] content = fileService.getFileContent(id); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", "file.bin"); return new ResponseEntity<>(content, headers, HttpStatus.OK); } } ``` 4. 在Vue中,可以使用axios来调用上述接口: ```javascript axios({ method: 'get', url: '/file/' + fileId, responseType: 'blob' }).then(response => { const blob = new Blob([response.data]); const url = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.setAttribute('download', 'file.bin'); document.body.appendChild(link); link.click(); document.body.removeChild(link); }); ``` 5. 在Element UI中,可以使用el-button来触发上述逻辑: ```html <template> <div> <el-button @click="downloadFile">Download File</el-button> </div> </template> <script> import axios from 'axios'; export default { name: 'DownloadFile', data() { return { fileId: 1 }; }, methods: { downloadFile() { axios({ method: 'get', url: '/file/' + this.fileId, responseType: 'blob' }).then(response => { const blob = new Blob([response.data]); const url = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.setAttribute('download', 'file.bin'); document.body.appendChild(link); link.click(); document.body.removeChild(link); }); } } }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值