I am using Cloudinary to upload document files (like Excel, DOCX, PDF) in my Java Spring Boot application. However, when uploading, the returned URL does not allow downloading the file in the correct format. Instead, it returns a file with no format or that cannot be viewed after dowload.
I use the uploadSingleFileToFolder method to upload the file:
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 the original filename
String originalFilename = file.getOriginalFilename();
if (originalFilename == null) {
throw new AppException("Invalid file name.");
}
// Remove the file extension for public_id
String filenameWithoutExtension = originalFilename.replaceAll("\\.[^.]+$", "");
// Upload the file with the correct resource_type
var options = ObjectUtils.asMap(
"folder", folderName,
"public_id", filenameWithoutExtension,
"use_filename", true,
"unique_filename", false,
"resource_type", "auto" // Set resource_type to "raw" for documents
);
var uploadResult = cloudinary.uploader().upload(file.getBytes(), options);
return uploadResult.get("secure_url").toString();
}
When I upload an Excel file (example.xlsx), the returned URL looks like this:
When accessing this URL, the browser downloads a file without a format (for example, example instead of example.xlsx).
I'm using free plan Cloudinary.
I have tried all those thing but still not working:
- Used resource_type: "auto" in options
- Checked returned URL (secure_url instead of url)
- Checked file has correct content type before uploading
Do you guys have any