Yêu cầu thg 2 26, 8:33 SA 76 0 0
  • 76 0 0
0

Làm thế nào để tải lên và tải xuống các tệp tài liệu (Excel, DOCX, PDF,...) lên Cloudinary và lấy được url để xem và tải xuống tệp chính xác?

Chia sẻ
  • 76 0 0

Em đang sử dụng Cloudinary để tải lên các tệp tài liệu (như Excel, DOCX, PDF,...) trong ứng dụng Java Spring Boot của mình. Tuy nhiên, khi tải lên, em nhận được url nhưng khi sử dụng url này thì nó qua 1 trang : "This res.cloudinary.com page can’t be found" và không có nào file nào được tải về:

ví dụ em upload 1 file tên example.xlsx thì url trả về là:

https://res.cloudinary.com/dndoreidh/raw/upload/v1740554207/project-document/example.xlsx và dẫn tới trang not found.

Em đã kiểm tra trên Cloudinary và file đã được upload thành công. Em đang dùng bản Free ạ.

Em sử dụng phương thức uploadSingleFileToFolder này để tải tệp lên:

public String uploadSingleFileToFolder(MultipartFile file, String folderName) throws IOException, AppException {
        if (file.getSize() > maxFileSize) {
            throw new AppException("File size exceeds the maximum allowed size.");
        }

        if (!isContentTypeAllowed(file.getContentType())) {
            throw new AppException("Unsupported file type: " + file.getContentType());
        }

        // Get original filename and extension
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null) {
            throw new AppException("Invalid file name.");
        }

        String extension = originalFilename.substring(originalFilename.lastIndexOf(".")); // Get file extension
        String filenameWithoutExtension = originalFilename.replaceAll("\\.[^.]+$", "");

        // Upload file as "raw" type (important for documents)
        var options = ObjectUtils.asMap(
                "folder", folderName,
                "public_id", filenameWithoutExtension,
                "use_filename", true,
                "unique_filename", false,
                "resource_type", "raw" // Raw is correct for non-image files
        );

        var uploadResult = cloudinary.uploader().upload(file.getBytes(), options);

        // Manually append the file extension
        String secureUrl = uploadResult.get("secure_url").toString();
        return secureUrl + extension;
    }

Không biết mọi người có cách nào để upload file tài liệu thành công, access được file hoặc tải được file đúng định dạng về không ạ?

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí