ng-file-upload使用注意事项

本文探讨了在使用ng-file-upload组件进行文件上传时遇到的常见问题,包括事件绑定方式、依赖项缺失以及DOM结构改变等。文章深入分析了这些问题的根本原因,并提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用ng-file-upload进行文件上传的时候,遇到很多不报错的奇怪问题,导致这些问题的原因:

1.使用了onclick=“”,这样的事件绑定方法。

2.没有安装flash(不支持html5浏览器)

3.使用了ng-if指令,其实就是改变了html的结构,若非要用ng-if实现dom元素隐藏,可以使用ng-show替换。

总结根本原因:就是html不能有错误,不能随便改变html结构,是否有运行环境。

//上传文件 @PostMapping("/upfile") public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile[] file){ fileUploadService.uploadFileToNas(file); return ResponseEntity.ok("File uploaded"); } @Service public class FileUploadService { private static final Logger log = LoggerFactory.getLogger(FileUploadService.class); private final SimpMessagingTemplate messagingTemplate; private final NasFileInfoMapper fileInfoMapper; private final NasDownloadLogMapper nasDownloadLogMapper; private static final String HOSTNAME = "10.32.5.80"; // NAS服务器地址 private static final String SHARE_NAME = "AnyshareBAK"; // 共享文件夹名称 private static final String USERNAME = "anyshare"; // 用户名 private static final String PASSWORD = "anybakup$*5.46"; private static final String NAS_UPLOAD_PATH = "/upload"; // NAS 目标路径,可根据需要修改 private static final String BASE_PATH = "/data/uploads/temp"; // 本地临时目录 private final SMBClient client; private Connection connection; private static Session session; public FileUploadService(SimpMessagingTemplate messagingTemplate, NasFileInfoMapper fileInfoMapper, NasDownloadLogMapper nasDownloadLogMapper) { this.messagingTemplate = messagingTemplate; this.fileInfoMapper = fileInfoMapper; this.nasDownloadLogMapper = nasDownloadLogMapper; this.client = new SMBClient(SmbConfig.builder() .withMultiProtocolNegotiate(true) .build()); } public void init() throws IOException { if (connection == null || !connection.isConnected()) { connection = client.connect(HOSTNAME); session = connection.authenticate(new AuthenticationContext(USERNAME, PASSWORD.toCharArray(), "domain")); } } private void uploadToNas(InputStream inputStream, String nasPath) throws IOException { init(); try (DiskShare share = (DiskShare) session.connectShare(SHARE_NAME)) { // 分离目录路径和文件名 int lastSlash = nasPath.lastIndexOf('/'); String directoryPath = (lastSlash > 0) ? nasPath.substring(0, lastSlash) : "/"; String fileName = (lastSlash > 0) ? nasPath.substring(lastSlash + 1) : nasPath; // 创建目录结构(如果不存在) createDirectoryIfNotExists(share, directoryPath); // 创建文件并写入数据 try (com.hierynomus.smbj.share.File nasFile = share.openFile( nasPath, EnumSet.of(AccessMask.GENERIC_ALL), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OVERWRITE_IF, // 存在则覆盖 null )) { try (OutputStream nasOutputStream = nasFile.getOutputStream()) { byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { nasOutputStream.write(buffer, 0, bytesRead); } nasOutputStream.flush(); } } } catch (Exception e) { log.error("上传文件到NAS失败: {}", nasPath, e); throw new IOException("NAS上传失败", e); } } private void createDirectoryIfNotExists(DiskShare share, String path) { String[] folders = path.split("/"); String currentPath = ""; for (String folder : folders) { if (folder.isEmpty()) continue; currentPath += "/" + folder; if (!share.folderExists(currentPath)) { share.mkdir(currentPath); } } } public void uploadFileToNas(MultipartFile[] files) { for (MultipartFile file : files) { if (file.isEmpty()) continue; String originalFilename = file.getOriginalFilename(); String nasPath = NAS_UPLOAD_PATH + "/" + originalFilename; try { // 生成文件哈希(可选) String fileHash = generateFileHash(file); // 直接上传到NAS uploadToNas(file.getInputStream(), nasPath); // 保存文件信息到数据库 NasFileInfo info = new NasFileInfo(); info.setFileHash(fileHash); info.setFileName(originalFilename); info.setFileSize(file.getSize()); info.setUploadTime(LocalDateTime.now()); info.setUsername(SecurityUtils.getUsername()); info.setDeptId(SecurityUtils.getDeptId()); info.setNasPath(nasPath); fileInfoMapper.insert(info); } catch (IOException e) { log.error("文件上传失败: {}", originalFilename, e); // 可以根据需要添加重试机制或更详细的误处理 } } } // 生成文件哈希(MD5示例) private String generateFileHash(MultipartFile file) throws IOException { try (InputStream is = file.getInputStream()) { return DigestUtils.md5DigestAsHex(is); } catch (Exception e) { log.warn("MD5算法不可用,使用随机哈希替代"); return UUID.randomUUID().toString(); }}} 前端会调用uploadFile触发文件上传(存在一次多个文件),service层中的代码为uploadFile的具体实现,当前使用SMBClient 的方式无法连接nas服务器以至于无法完成后续文件传输的过程,所以我希望换一种方式,使用jcifs-ng库完成以上功能,请在我的原代码上做出修改
07-23
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值