admin管理员组文章数量:1432601
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
本文标签:
版权声明:本文标题:java - How to upload and download document files (Excel, DOCX, PDF,...) to Cloudinary and get the url to view and download the c 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741148039a2347436.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论