I have problem with downloading a pdf file from a particular link.
For example I have a URL like http://<host>:<port>/alfresco/service/content.pdf
and in Liferay I want to download a file from this link.
I tried this:
resourceResponse.setContentType("application/pdf");
resourceResponse.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
resourceResponse.addProperty(HttpHeaders.CONTENT_DISPOSITION,"filename=" + url);
OutputStream out = resourceResponse.getPortletOutputStream();
Related
i'm trying to download a something from minio browser with "a" tag while using download attribute.
in this case i want to download a song, when i click on link, i redirect to play song page, but i see no download or save as pop up.
if i copy the link and past that in idm(internet download manager), start to download the file.
i set the bucket policy => read only for *
it is the download link that generated
https://5c6d9b4556185a0011c13b92.storage.liara.ir/singles/Ali%20Sorena%20-%20Aavaar(320)?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=V53B3X6JUVA1NSMG7SOAJ/20190417/us-east-1/s3/aws4_request&X-Amz-Date=20190417T104438Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=39db9a3041d351b03c7b71b8a68d37f1729374e9008be9a68d378f88fb043b50
and a tag:
<a href="https://5c6d9b4556185a0011c13b92.storage.liara.ir/singles/Ali%20Sorena%20-%20Aavaar(320)?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=V53B3X6JUVA1NSMG7SOAJ/20190417/us-east-1/s3/aws4_request&X-Amz-Date=20190417T104438Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=39db9a3041d351b03c7b71b8a68d37f1729374e9008be9a68d378f88fb043b50"
download>Download</a>
You will need to add a Content-Disposition header to force download (see How to Use Content-disposition for force a file to download to the hard drive?).
You can apparently use the reqParams argument to add response-content-disposition.
Something like this, maybe.
liaraClient.presignedUrl(
"GET",
"mybucket",
"myfile.mp3",
{ "response-content-disposition": "attachment; name=myfile.mp3" },
24 * 60 * 60,
(err, presignedUrl) => {
if (err) return console.log(err);
console.log(presignedUrl);
},
);
I created a word document using Apache POI API and can write the file to my local machine and it works very well but when writing to response.getOutputStream() to be able to allow the user to download using the browser, the word document gets corrupted.
Here's my code:
String docFile = "inv_export_012.docx";
FileOutputStream fos= new FileOutputStream("C:\\temp\\" + docFile);
//set response headers
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.setHeader("Content-Disposition","attachment; filename=" + docFile);
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "0");
//create docx format using XWPFDocument
XWPFDocument wordDocument = new XWPFDocument();
XWPFParagraph p1 = wordDocument.createParagraph();
p1.setAlignment(ParagraphAlignment.LEFT);
XWPFRun r1 = p1.createRun();
r1.setText("Invoice Number: 00000000XXX");
r1.addCarriageReturn();
//write document to local C: drive
//this step works well and I can successfully create the document
wordDocument.write(fos);
//set content length
response.setHeader("Content-Length", String.valueOf(new File("C:\\temp\\" + docFile).length()));
fos.close();
wordDocument.write(response.getOutputStream());
Also tried the binary formats and it doesn't work either.
response.setContentType("application/octet-stream");
Java version: 1.6 (Can't update to new version yet)
Apache POI: 3.16
Browser: Internet Explorer 11 (My application is only certified in IE so can't use other
browsers)
Office 2013
It works well and document is created successfully on my local desktop. If I try to download using the browser and open it directly (or save it and open it), below error appears indicating document is corrupt. Clicking to open the document still opens the document but anyone knows why I am getting this message? Any help would be appreciated!!
Clicking OK I get this message:
If I Click 'Yes' it does open the document.
EDIT: 06/29- Updated the code to generate *.docx format. Also added Content-Length in the response header. Tried by removing the Content-Length too both of which didn't work.
Found out that POI 3.16 has this problem, this issue is resolved when I upgraded to Apache POI 4.0.0 jar.
I'm making an extension in which the user is setting some configs and, based on his selection, a png file needs to be downloaded (from a remote url) in the background and saved as a file somewhere to make it accessible later. It must be saved as a file and also injected in the page by it's final path.
Reading the [fileSystem][1] api of chrome, all methods are sending a prompt to the user in order to download a file. Is there any way I can save files to a location without prompting the user for download?
In an effort to close this years old question, I'm quoting and expanding on wOxxOm's comments and posting as an answer.
The key is to download the raw data as a blob type using fetch. You can then save it via an anchor tag (a link) using the download attribute. You can simulate the "click" on the link using JavaScript if you need to keep it in the background.
Here's an example (more or less copied another SO post).
const data = ''; // Your binary image data
const a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
const blob = new Blob([data], {type: "octet/stream"}),
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName; // Optional
a.click();
window.URL.revokeObjectURL(url);
This is not without its faults. For instance, it works best with small file sizes (exact size varies by browser, typically ~2MB max). The download attribute requires HTML5 support.
I have followed some steps for converting a Spreadsheet to XLSX. But when I try to open the exported and sent file, it says "file or extension not valid". Checking it as an HTML, it says "The file doesn't exist".
If I try the file URL used in the code, directly in browser, it downloads the correct file as XLSX.
Attached you can find a screenshot of the code and a screenshot of the downloaded html.
HTML result when trying to save spreadsheet in excel
Implemented code
Thanks,
I haven't done specifically what you are doing, but I have exported from google docs to other formats. I always do it by using createFile(blob) after specifying the blob name. Something like this:
var response = UrlFetchApp.fetch(url, params);
var folder = DriveApp.getFolderById('target folder id');
var doc = 'file name';
var blob = response.getBlob().setName(doc);
var file = folder.createFile(blob);
var fileUrl = file.getUrl();
Then include the fileUrl variable in your email.
I have searched and tried all of the solutions at stackoverflow but none of them seems working at my instance. Basically I have an image, edited by html5 canvas, upload from client and I need to save it to disk, but unfortunately I can't open the file that I just saved. (I am using Windows 7)
My code:
var base64Data = req.body.image.replace(/^data:image\/(png|gif|jpeg);base64,/,'');
require('fs').writeFile('public/aboutToGiveUp.png', new Buffer(base64Data, 'base64'));
Got same bug, it's because of incorrect url path, You may added app.use("/", express.static(path.join(__dirname, 'public')));, so no need to add the public in the url, check your url path once.
working sample :
url = req.protocol+'://'+req.headers.host+"/"+filename;
url = req.protocol+'://'+req.headers.host+"/images/"+filename; // its in public/images
Try using ./public/aboutToGiveUp.png or make sure that the path is relative to the file containing this code.