Insert Image from Symfony path with PHPExcel - symfony

I'm trying to insert images in my Excel using the PHPExcel_Worksheet_Drawing of PHPExcel (in this Symfony Bundle) but I get this message all the time:
File http://benefest.local/uploads/persons/image/58af244d3f58c_16143190_10210682923503464_8658615512523719175_n.jpg not found!
Here is my code:
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath($path);
$objDrawing->setWidth(30);
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($phpExcelObject->getActiveSheet());
My images are uploaded by VichUploaderBundle, and I'm generating the absolute path through a function. which returns me http://benefest.local/uploads/persons/image/58af244d3f58c_16143190_10210682923503464_8658615512523719175_n.jpg
In a browser, this image is displayed!
Any clue?

Answer is based on my comments to this question.
There is a difference between URI and file path:
URI - used in browser, or being more general in HTTP protocol to get access to some resource via web-server
File path - a link to some file within filesystem on your server/computer/etc.
Your function returns a URI string (http://benefest.local/uploads/persons/image/58af244d3f58c_16143190_10210682923503464_8658615512523719175_n.jpg), which can be used to access the image via a web server.
But PHPExcel_Worksheet_Drawing as well as other libraries use PHP's filesystem functions to get access to some file locally. So you need to get a local file path, instead of URI (in you case it is ./uploads/persons/image/58af244d3f58c_16143190_10210682923503464_8658615512523719175_n.jpg).

Related

How to access html file inside iframe in production

I am creating a web app where, this app will generate a .html file in public/ directory. And after that i want to show that generated html file on a page using iframe .
I am using NextJS for this task. I have configured next.config.js for rewrites but getting 404 error. I am using Railway for hosting. Since in Vercel we can not access filesystem here.
Instead of generating html dynamically, if I upload that .html file, then i can access that file using iframe in production.
I think we can not access those files which were not available during build process. (this is my guess , maybe wrong)
How can I solve this problem or should I use another framework ?
Thanks
Since I was accessing .html files (i.e <iframe src="path-to-file"></iframe>) which were not available during build process, that's why i was getting 404 error.
So I did the following thing:
suppose I want to do this <iframe src="/pdf/page01.xhtml"></iframe> where file location is in public/pdf/page01.xhtml.
so this src is sending a GET request to localhost:3000/pdf/page01.xhtml.
To manipulate this request I created a pages/api/pdf/[...slug].js. In this file you can use process.cwd() to access the file inside your public dir. You read file content using fs.readFile and send the response res.status(200).send(data) like this.
By doing this I was getting the desired result.
Suggestions are always welcome.

Force file download in a browser using ASP.Net MVC when the file is located on a different server without downloading it on my server first

Here's what I would like to accomplish:
I have a file stored in Windows Azure Blob Storage (or for that matter any file which is not on my web server but accessible via a URL).
I want to force download a file without actually downloading the file on my web server first i.e. browser should automatically fetch the file from this external URL and prompts the user to download it.
Possible Solutions Explored:
Here's what I have explored so far (and why they won't work):
Using something like FileContentResult as described here Returning a file to View/Download in ASP.NET MVC to download the file. This solution would require me to fetch the contents on my server and then stream from my server to the browser. For this reason this solution won't work.
Using HTML 5 download attribute: HTML 5 download attribute would have worked perfectly fine however the problem is that while it is really a very neat solution, it is not supported in all browsers.
Changing the file's content type: Another thing I could do (at least for the files that I own) to change the content type property of the file to something that the browser wouldn't understand and thus would be forced to download the file. This might work in some browsers however not in all as IE is smart enough to go beyond the content type and sees the file's content to determine the content type. Furthermore if I don't own the files, then I won't have access to changing the content type of the file.
Simply put, in my controller action I should be able to specify the URL of the file and somehow browser should force download the file.
Is this something which can be accomplished? If yes, then any ideas how I could accomplish this?
Simply put, in my controller action I should be able to specify the URL of the file and somehow browser should force download the file [without exposing the URL of the file to the client].
You can't. If the final URL is to remain hidden, your server must serve the data, so your server must download the file from the URL.
Your client can't download a file it can't get the URL to.
You can create file transfer WCF service (REST) which will stream your content from blob storage or from other sources through your file managers to client browser directly by URL.
https://{service}/FileTransfer/DownloadFile/{id, synonym, filename etc}
Blob path won't be exposed, web application will be free from file transfer issues.

Get the uploaded document url through alfresco

I am very new to alfresco(using version 3.4). I have written a document library action to validate the uploaded file. Now I want path of uploaded file so that I can pass it to my service to validate. Can any one tell me how could I get the downloadable path of a document ??
THanks....
I don't know how you've build your action. But on the client side Javascript you could access file.path.
On the repository side webscript, the normal action imports action.lib.js.
In which the path variable is available.
If you take a look at e.g. Backup Action in the googlecode project share-extra's then you'll probably will know what I mean.
The URL download path?
If you know the nodeRef of the document, you can construct the path yourself.
Here's an example:
http://localhost/share/proxy/alfresco/api/node/content/versionStore/version2Store/169c2842-6bc8-4ace-9b9f-1a46aebb100b/MyFile.pdf?a=true
Everything up to and including the "version2Store" is fixed, and then you have the nodeRef/file part. That's the download link for a document (assuming the user is logged in and can have access to the document or the document is accessible for guests.

download option window in jsp

I need to give an option to user in jsp to choose a folder where he can save/download a file. Please help me on the same.
the text input="file" will give the file chooser but i need the directory chooser
HTTP doesn't allow you to specify (server side) where a file is downloaded to - this is not a jsp specific thing.
If you need to this then you'd need to provide an embedable application (javascript, java, flash, vbscript...) which is allowed to operate outside the browser sandbox and implements its own network client for retrieving the file. Which is far from an ideal solution.
You can force the download to use a specific name via the content disposition header.
the text input="file" will give the file chooser
..but that's for uploads - not downloads.
You can't set folder location at client machine of downloaded file using JSP/Servlet. If you want to add folder chooser feature then you have to develop an applet. You may use JFileChooser to allow user to select a folder and java.net.URL and java.net.URLConnection to download a file.
Most browsers will automatically download a file that the browser doesn't render, so it's just a link...! For example, if it's a zip file, just add it as any old "a link" in your code. When the user clicks , the download/save dialog will be launched ....
The "save/download" feature is a client issue -remember the web developers job is to provide content- it's the browser that decides how to deal with the content.
The key is the Content-Disposition header. Its value has to be set to attachment to force a Save As dialogue. You can do this job in a servlet. Just let your link URL point to a file servlet like so
download filename.ext
Then, in the file servlet, which is for the above example to be mapped on an URL pattern of /fileservlet/*, do the following:
String filename = URLDecoder.decode(request.getPathInfo().substring(1), "UTF-8");
response.setHeader("Content-Type", getServletContext().getMimeType(filename));
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
// Now get an InputStream of the file and write it to OutputStream of response.
See also:
Simplest way to serve static data from outside the application server in a Java web application

How to get the path of the context directory in jsp and how can i overwrite it for every new request

Hi i am getting path of the context directory in my local system using..
String myfile = application.getRealPath("/");
but the method getRealPath("/") is returning null when application is deployed in war file in www.eatj.com..
can any one provide me possible solution and sample code please...
The purpose is i have to create a xml file in my context directory...
and each request i have to overwrite this xml file...
This is what Javadoc says
Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext..
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

Resources