How to access JSF folder resources/images programmatically in backing bean? - mysql

I'm trying to save/insert the image by default in one of the column record during the user registration process.
The image source is in WebContent > resources > images of the web project in Eclipse IDE.
I need to access the said image in the ManagedBean or DAO class in order to insert the image value in the DB(MySQL) as a default value.
Thanks in advance.

You can use getResourceAsStream method on External context to read the file from resources folder.
InputStream iStream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/resources/images/YOUR_IMAGE_NAME");

Related

How to insert a downloaded image into HTML code?

I'm a junior dev trying to build a portfolio site and want to upload a pic of me that is already downloaded on my PC. When I type in an src of an image that exists somewhere online it clearly appears on my live server but when I insert the src of an image existing somewhere within my PC's local drives it doesn't appear. Is it that I have to upload this image somewhere online (e.g. google drive) and then type in its new src or is there a way to upload offline images?
Hope someone can help!
Create a folder called assets in your project and add all images and any other files you may need. Then add the path to your image in src
there are some options to do it.
The more preferable is creating a folder let's say Src or Images. Then download any images into the folder using classes from a namespace System.IO (Path, Directory, ...). But be sure that your image names+extension are unique. Also, you can store in a DB file name and path the related file on your server and userId to be sure who added the file.
Another option is to store byte arrays in DB. Adding action for returning your file
public async Task<AcitonResult> GetImage(int id)
{
byte[] imgBytes = await GetImageFromDbById(id);
if (imgBytes == null)
return NotFound();
return File(imgBytes, "image/jpg");
}
Asking an image
<img src="/**YourControllerName**/GetImage/**#imageId**">

How to access current folder in a Forge - Design automation app

I have created a forge app, for revit, using the provided deleteapps design automation template, and the design automation API. The goal of this app is to process and export content from revit file.
My question is, how to get the current working directory, in the app, when a workitem starts?
public void HandleDesignAutomationReadyEvent(object sender, DesignAutomationReadyEventArgs e)
{
e.Succeeded = true;
Export(e.DesignAutomationData);
}
There is ModelPathUtils.ConvertUserVisiblePathToModelPath(model); but I dont want a path to a revit file, just a base path, where I can create folders.
Design Automation allows you to write to current working folder. It is a unique path for each job. You can call Directory.GetCurrentDirectory() to access the current folder.
string path = Directory.GetCurrentDirectory();
This will give you the root folder for a given job. You are allowed to create sub folders and save files within this folder.
You can also take a look at the current RVT Document PathName property. Not sure whether it lives in the current directory, though, but it might be interesting to check.

fuelphp download csv on button click?

Similar to the uploading feature in fuelphp (link provided below), is there a tutorial for downloading files in fuelphp. There is not much information out there for fuelphp (other than the docs). Would I require a separate config page called download.php similar to upload.php?
All I really need is a page with either a download link or button to export csv to a user's local machine
Link to Upload feature
https://www.tutorialspoint.com/fuelphp/fuelphp_file_uploading.htm
Thanks in advance
Have you looked at the File class? This has a method of download which you pass the path of the file to. The 2nd param allows you to specify the name of the file that's downloaded.
File::download('path/to/file.txt, 'new-file-name.txt');
If you want to restrict downloads per user, you'll need to add logic around that.
https://fuelphp.com/docs/classes/file/usage.html#/method_download
Example
Create a new controller, as you've pointed out, download.php, and use the following as a starting point.
You'll need to pass something through to the get_index in order to determine which file the client will download. I'd suggest some sort of look up, using an unique identifier, instead of a file path, otherwise this would easily be exploited.
class Controller_Download extends Controller
{
public function get_index()
{
// #todo add logic surrounding file download
File::download('path/to/file.txt, 'new-file-name.txt');
}
}

Explore for files in Access

I have a form that a user uses to upload a CSV file. Right now the user has to type the full file path of the file into a text box. How can I get a browse for file popup to allow the user to navigate to the file instead of having to type the whole path? Is the functionality already present and I just don't know how to use it yet?
you can create your own class with API calls. Here you have the code. Or you can add a reference to Microsoft Office xx Object Library and use the FileDialog class.
Here you have an example of usage.
I prefer the first method, because you are not attached to an external library.

Display .RDLC report embedded in a DLL file

I have a report that is used by a windows service and a form application. So, I want to put embed the report in a DLL file that can be used by both.
The problem is that if I try to set the ReportEmbeddedResource property of a ReportViewer control in my windows form app, it will search the windows form app for the resource, not the dll file.
e.g.: Code from the windows form app:
rv.LocalReport.ReportEmbeddedResource = "MyReportInMyDLLFile.rdlc"
How can I make the above command look for the embedded resource in my DLL file?
Something like this should do it:
Assembly assembly = Assembly.LoadFrom("Reports.dll");
Stream stream = assembly.GetManifestResourceStream("Reports.MyReport.rdlc");
reportViewer.LocalReport.LoadReportDefinition(stream);
Just use the full namespace of the assembly, then folder names and then the name of the file:
rv.LocalReport.ReportEmbeddedResource =
"My.Assembly.Namespace.Folder1.Folder2.MyReport.rdlc";
Then make sure the report file is set as an embedded resource using the properties pane.
Probably the best thing to do would be to get a stream to the RDLC resource from the other assembly, then pass that to the "LoadReportDefinition" method of the Report Viewer control.
Details of how to get a stream from an embedded resource in a different assembly can be found here : Retrieving Resources with the ResourceManager Class
Additionally, you will need to refer to the embedded resource using it's full namespace path.
E.g. if you have an application with a default namespace of TheApp, and you keep a report called "MyReport.rdlc" in a folder called "Reports", the report reference call would be:-
rv.LocalReport.ReportEmbeddedResource = "TheApp.Reports.MyReport.rdlc";