Reputation: 307
How can I download pdf file from azure file storage. I know that there is a method downloadText()
but it corrupts the pdf file. I have written code to download a uploaded file on azure and I have used downloadText()
method but it's not working here.
However it downloads the pdf file but it says file corrupted or damaged.
Code sample which I am trying to do:
CloudFileDirectory sampleDir = rootDir.getDirectoryReference(path);
//Get a reference to the file you want to download
CloudFile file = sampleDir.getFileReference(fileStorageBean.getUniqueFileIdentifier());
//get file contents from azure file.
String fileContent = file.downloadText();
ESAPI.httpUtilities().setHeader(response, "Content-Disposition","attachment;");
response.setContentType(fileStorageBean.getMimeType());
response.setContentLength(file.getStreamWriteSizeInBytes());
InputStream in = new ByteArrayInputStream(fileContent.getBytes());
Upvotes: 0
Views: 778
Reputation: 23782
You could use file.downloadToFile
method to download your pdf
file from Azure File Storage
to local.
Sample Code:
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
CloudFileClient fileClient = storageAccount.createCloudFileClient();
CloudFileShare share = fileClient.getShareReference("test");
CloudFileDirectory rootDir = share.getRootDirectoryReference();
CloudFile file = rootDir.getFileReference("testFile.pdf");
File sourceFile = new File("E:\\AzureFile\\f.pdf");
file.downloadToFile(sourceFile.getAbsolutePath());
Or you could use file.download
method to download file to a stream.
OutputStream outs=response.getOutputStream();
file.download(outs);
Hope it helps you.
Upvotes: 1