I want to insert an image to database and display it in another page. I am using the PostgreSQL database.
My guide suggests that I insert the image with its file path in the database. When displaying, in place of the src attrib of img tag put the path from database. So can I get any help for this .
Please guide me for this or give me link for similar kind of problem.
(I'm a final year student, and feel that this project requirement is difficult).
Your guide is entirely correct. Part of what you are supposed to be learning is problem solving: how to break a big problem down into many smaller, simpler problems you can solve piece by piece. It sounds like it's hinting at this, but expects you to be able to do that yourself, which is pretty reasonable.
You need to break this down into steps, and do each step in isolation. That's how anything but the most trivial programming task must be done.
(It isn't clear if you want to store the image data in the DB, or just a file system path, by the way, so I'm assuming you want to write the file to the local file system and just store the path in the DB).
Anyway, this should be fairly simple JSP. To display:
One JSP that:
Examines the query parameters for the image ID
Uses JDBC to fetch the associated path of the image on the file system from the database (a simple SELECT using the image ID as a query parameter)
Opens the image on the file system as a binary stream; also stats it to get its size
Sends appropriate HTTP headers eg Content-Type: image/jpeg and Content-Length: image-length-in-bytes to the client
Copies the raw image data from the image input stream to the output stream that sends to the client
Another JSP that generates the HTML and has an <img src="/the/image/jsp?imageid=blah"> link in it.
If you're required to submit just one JSP file, you can combine the two by having the JSP show a HTML page if it doesn't receive any query parameters, and send an image if it does receive an image id as a query parameter.
To insert:
One JSP that displays a HTML form with a file upload link if it doesn't get called with any HTTP POST data
If the JSP does get called with HTTP POST data:
** Issue a JDBC INSERT to create a record for the file in the database, but do not commit
** Access and decode the POST data using the methods provided in JSP
** Extract the desired file name from the form data and open a binary output stream to a file on the filesystem with that name
** Copy the image bytes into that output stream, url-decoding if required (the HTTP POST form handling code in JSP is likely to decode it to a byte stream for you, though)
** Flush and close the output stream
** Commit the transaction with the JDBC INSERT.
You should be able to find numerous examples of both with a quick Google search. If you can't, adapting examples from other programming languages should be easy enough.
For inserting you must think carefully about the error cases. That's a large part of proper programming.
I am intentionally not showing you code examples. You should be able to do this yourself if you're a final year student. You won't know everything you need, but by now you should know how to find out what you don't know when you need to know it. Tutorials. Documentation. Google. Writing test programs to figure things out. Method name autocomplete in NetBeans / Eclipse. Adapting sample code. You've got lots of options.
Related
My code (view) So far everything is working well when am accessing a folder(uploads) withing the root, but i would like to access a folder located in a different location withing the same server without showing sensitive information in the url when the image loads.
<img src="<?php echo base_url('/uploads/'.$popular_car['img_path'])?>" class="card-img-top"
style="height: 150px;"></div>
There's many alternatives. Most will involve some sort of database use. This is what I do (greatly summarized)
Every file that will need to be accessed has a record on a table. The record has a primary id, a secret random token and the path to the file. The table is indexed by both the primary ID and the token.
On the URL I get something like base_url('controller/file_access/).$id.'/'.$token. Upon receiving the request, I'll check the files table, if there's a match for both parameters, I'll stream the file to the browser. This way of doing things, albeit a little bit more complicated, has two main benefits:
1.- it prevents a user to just try different numeric IDs and see what is displayed. Since IDs are numeric and autoincrementing, all you'd need to do is looping from 1 to 100000 and download all files. Adding the token and querying the table with both parameters greatly reduces the risk of someone getting a file he/she's not intended to.
2.- it obscures the real location of the file, as streaming it to the browser in this way looks (in the eyes of the browser) as being located in example.com/controller/file_access/id/token but the real or relative path to the image remains hidden and non-accessible from the web.
Using this as a base you can add a lot of logic on top of this depending on your needs. You could also have an "allowed_user" field in the table if files are private and/or user-specific so that you don't stream the file if the user is not allowed to see it (even if he has the correct ID/token combination).
This is just a rough description of what I do on a couple of sites. Take it as the theoretical foundation you can build on.
Halo ! I'm trying to implement dropzonejs in a very specific way. Actually I follow the standard implementation described on the official page. Everything works perfectly.
But I'm willing to attach the server's generated URI for each uploaded file directly when uploaded : when uploading it's creating a database entry with some stuff like a page uri with title etc. This mean that the server would return as a response the id of the database saved file in order to attach the href attribute with its value to the the element in front.
This is quite ok to do this when only one file is uploaded, but it becomes trickier when bulk uploading.
So maybe I didn't understand the documentation well (and I'm quite sure I didn't), but is there any way to add custom data-dz-like attributes based on my server's response ? I'd like something like data-dz-url where the url points to a database entity (not the file itself).
Or if not if there is an "easy way" to handle this.
Thanks a lot
Here is the answer :
myDropzone.on('success', (file, response) => {
file.previewElement.href = "/admin/media/"+response.id+"/show/"
})
file is reference to the current uploaded element. It's possible to extend it's html attributes through previewElement. Setting the data-type attribute in the template before, then assigning it the right value works aswell.
Hope this will help some.
Is there another way to view the profiling results of MiniProfiler (I'm specifically interested in EF5 version)?
Every tutorial that I've seen uses MiniProfiler.RenderIncludes(); but since my MVC app mostly returns JSON, that is not an option for me.
Is there a way to write results to file or something like that?
You can read and write results to just about anywhere by changing the MiniProfiler.Settings.Storage to a different IStorage implementation from the default (which stores to http cache). If you wanted to, this could store to and read from a file pretty easily (you would have to write your own custom implementation for that).
The files served by RenderIncludes are the html templates for displaying the results and the script to retrieve the results from the server and render them on the client (all found here). But you are by no means obliged to use this mechanism. If you want to write your own logic for retrieving and displaying results, you should base this off of the logic found in MiniProfilerHandler.GetSingleProfilerResult. This function roughly performs the following (putting in the siginificant steps for your purposes):
Gets Id of next results to retrieve (through MiniProfiler.Settings.Storage.List())
Retrieves the actual results (MiniProfiler.Settings.Storage.Load(id))
Marks the results as viewed so that they wont be retrieved again (MiniProfiler.Settings.Storage.SetViewed(user, id))
Converts these to ResultsJson and returns it
With access to MiniProfiler.Settings.Storage, you should be able to retrieve, serve and consume the profile results in any way that you want. And if you are interested in using the RenderIncludes engine but want to mess around with the html/js being served, you can provide your own custom ui templates that will replace the default behavior.
Saving the image in a web directory and storing the URL in the database using this approach, I stored the image URL in the database. Based on that image id (I need to pass this image id to the controller from an Ajax call). I need to retrieve the image.
I got the image id using a jQuery template, so I have passed that image id to the controller. What should I write in the controller, filepathresult or fileresult? Or is there another approach?
OK, you're a bit confused.
You have the actual image file, file.jpg and you have the physical path to the file, D:\some\path\to\file.jpg.
You have the URL path to the file and a surrogate identity (your id).
id: 1337 (some random number)
URL: ????
First question:
You say you're storing the URL. Is it really the complete URL? Is it just a partial path to the image? Is the path from the root of the website or the root of the application? Is it just a partial physical path?
Second question:
What are you actually trying to do?
Do you just want to get the full path to the image? Why do you need Ajax to do this, if you already have the id? You might want to rethink how you're storing the images if any performance needs to come out of this.
Once an image gets a new identity, it often makes sense to use that new identity everywhere; you might ought to consider copy/rename the file for the new identity after it's uploaded (and possibly save the old filename for record keeping purposes). If you need to keep the file names (more or less) as-is, however, it'd be better to provide the ability to grab the URLs for a whole set of ids rather than to individually make an Ajax request id-by-id.
If you request a resource (AKA navigate to a URL) that has a physical file, IIS is going to serve it directly (that is, if you ask for www.mysite.com/Images/Image3.jpg IIS is going to serve it directly). I really don't understand exactly what you are doing, but if you mean that you get the associated URL for an image using an Ajax call to an MVC controller with the id of the image, you could do several things.
You could simply return the URL and use JavaScript code to create an image tag with that URL and inserting it in the DOM.
You could return a view like <img src="{yoururl}" /> and insert it in the DOM using JavaScript.
You could store the images on the database directly and use the File method to return the image bits indicating the correct MIME type.
I'm new in Spring MVC, I just started my first project and I'm doing some research to be sure to set it up in a proper way (should work in the long-term!).
I already know that for a part of the project, I will need to manually change small fragments of the page through Ajax. I know it's possible to change part of the page (using Tiles). What I really need, though, is for example to change a single line in a table containing dynamically generated data (i.e. data coming from the database).
Can you suggest anything?
I don't want to use JSF or Spring JS.
Thank you.
You have at least two choices:
render on the server, send the update html snippet to the brower and use JavaScript to replace them
send an AJAX request to the server, but this time return only the data (JSON) and the "render" the table line in the browser (or just update some pices of text)
For the fist choice you need a dedicated jsp file (and tiles configuration) to render only a single line. As fare as I know, there is no technical support.
What you can do, to reduce the amount of duplicated code is to use that single line rendering jsp in like in include in the one that renders the complete table.
Of course instead of using JSP to render the single line you can also use the Java Method that handles the request, and make it returning the html string.