Add binary data to html form for upload - java

Background: I have to support multiple file uploads in IE7-9. I've found uploadify and FileReader which are both flash based.
Our current file upload allows the user to select a file, type in a description and check some check boxes. That data is all sent to the upload servlet at the same time and the servlet gets the file data and the description and the checkbox values and stores the file on the server and adds an entry into the database.
The problem: uploadify and FileReader both want to send the file directly to the server, I don't have a chance to add a description or set any flags. I've worked with FileReader some now and can intercept the file instead of sending it to the server. What I would like to do is get the binary data and put it into the form, let the user add the description and then submit the form with the binary file data.
I've all ready tried just adding a hidden field to the form but the data didn't seem to come through.
If worse comes to worse I think I could just upload the file and then update the database when the form is submitted, I don't want to do that but I think that would work.
Does anyone know of anyway to add the file data to the form and then to get the servlet to recognize that data as part of the form?

You can pass data along side your upload in Uploadify, just use the formData attribute like this (found here):
<input name='someKey' type='text' value='Some Value'/>
<input type="file" name="file_upload" id="file_upload" />
<script>
$('#file_upload').uploadify({
// Some options
'method' : 'post',
'formData' : { 'someKey' : $('input[name=someKey]').val() }
});
</script>

Related

How to enter data from excel sheet to mysql database using JSP? JSP + Servlet + MySQL Project

So, basically what I want to do is, on a jsp page give option to browse and select an excel file. Then when the user clicks on Upload button, the data from excel is saved into database table.
I am able to insert into database table without the UI i.e., when there is only servlet and database. In that case, I am doing this:
InputStream fis = new FileInputStream(new File("C:\\Users\\RAJYAWARDHAN\\Desktop\\Book1.xlsx"));
And after this I use fis to one by one enter data into Database table using Apache POI. But when JSP page is included I am using form:
<input type="file" name="file_uploaded" />
So, when not using JSP page, I have the address of file, that is on my machine only. But that's for test purpose. When user will upload, then I will not have the address of the file because file is on users machine. Hence, I cannot use new File("address_of_file") as file is not on my machine(server basically).
Also, I don't want to first upload file from users machine to my machine and then pick it up from that particular path. I just want to import data from the selected file by user and not actually upload and save that file.
What should I do? Please explain in simple terms. Thank You.
Please ask if question is not clear. English is not my first language.
I think what you want to use is getPart method in servlets.
Just like request.getParameter you can use request.getPart("file_uploaded"). It gets file for the given name from front end(jsp/HTML).
This returns Part object which can be converted to InputStream. You don't need to save file to some location in this case.
Do not forget to forget to set enctype= multipart/form-data in your form.

JSP page - In a table <TD><input type = text name = "code"></TD>

I have created a JSP page with a table containing two columns Code and Description ,the table data is input type="text" name ="code", the functionality which i have to implement is,if i type the code number the description of the code has to generated automatically from the database could any one please help me
Why not you create a javafunction that contain a ajax call to your servlet or java file where you can write a code to get description based on code value and return back to jQuery function.
You can trigger jQuery function on "OnChange" event of code input box.
You can get various example and docs over google about ajax request.

Editing text file dojo

I am learning dojo and a begginer. I would like to load and edit a text file from interface using Dojo. Please direct me which component in dojo would suffice this.
From the answer to my comment, my understanding is that you want to:
Enter a file name into a form on your webpage.
Dojo will then load that particular text file from your sever.
The file then needs to be displayed on the screen in some sort of editing componant.
The user then needs the facility to post the file back to the server.
I would have thought that the best approach is to use dojo/request to get the text file and then use it again to post it back. You could the various dojo dijits to do the selecting and displaying.
A very crude solution would be:
<form data-dojo-type="dijit/form/Form">
<input type="text" id="fileName" data-dojo-type="dijit/form/TextBox" />
<button data-dojo-type="dijit/form/Button" type="button">Get
<script type="dojo/on" data-dojo-event="click">
require([
"dijit/registry",
"dojo/request"
], function(registry, request) {
var fileName = registry.byId("fileName").get("value");
request(fileName, {
"handleAs": "text"
}).then(function(content){
registry.byId("content").set("value", content);
});
});
</script>
</button><br /><br />
<textarea id="content" data-dojo-type="dijit/form/TextBox"></textarea>
<button data-dojo-type="dijit/form/Button" type="button">Send
<script type="dojo/on" data-dojo-event="click">
require([
"dijit/registry",
"dojo/request"
], function(registry, request) {
var content = registry.byId("content").get("value");
request("myhandler.php", {
"method": "post",
"data": {
"content": content
}
}).then(function(content){
// deal with the response
});
});
</script>
</button>
</form>
This will load a text file with the filename you enter in the text box (after clicking get). The content is loaded into the textarea for editing and can be sent back to a server script by clicking send.
This is as I said, "a very crude example". However, it shows the use of dojo/request to receive and post information to/from the server. Obviously, you'd want a more sophisticated solution that hides/shows widgets at the appropriate moment. You would probably want to replace the filename textbox with a some sort of combo populated via your server code...etc, etc.
I'd suggest you write your own widget to encapsulate the whole operation, rather than declare it all in markup. You could use dojo/request to load a json file from the server to populate a combo box to select the file. You'd also want to ensure the information being posted back is from a trusted source.
Important! This will only work if the textfile and you webpage are residing on the same domain. It will not work for cross-domain requests. If you want to do cross-domain you'll need to create a json solution.

Strange behavior with the method getUploadedBlobs

I've a problem with the methode blobstoreService.getUploadedBlobs(). I've a JSP page in wich one I set an uploader like this :
<formname='form' action='<%= blobstoreService.createUploadUrl("/Edit_Engine") %>' method='POST' enctype='multipart/form-data' >
<input label='...' multiple='false' name='myFile' />
//...and multiple input for text
</form>
and I retrieve this code with my servlet :
java.util.Map<String,BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobK = blobs.get("myFiles[]"); //I don't know why I need to add the characters 's[]' at the end...
But the behavior is strange. The first time I upload an image, everything works. However, the second time, I send my form without uploading somehting (only text data), and then my java code finds a BlobKey. But this BlobKey seems to be the previous sended data, or a corrupted data.
I mean that not normal, because when I deploy this version on my localhost, if the form uploads no file the method getUploadedBlobs returns an empty HashMap. However, when I deploy on google servers, if the form uploads no file, the method getUploadedBlobs seems to return a HashMap with wrong data.
Could you help me? Or tell me if this behaviro is normal...
Many thanks,
bat
If you're getting a valid BlobKey, then myFiles[] is most likely the name given to the file input field in the form. Is that the case? That seems like an odd name for an input field. Are you using a template library to help generate HTML from the JSP?

send large text to server using html form hidden value

HI all
I need to send an dynamically generated html to server using html form, html can be bigger size at present it is 1MB
I m sending an dynamical generated html to server using form hidden input field. at server side exception is : too large content..
The dynamically generated html is used to generate pdf and generated pdf will send back to browser in same request of response.
How to handle bigger size html which is generated dynamically.
Please help me out.
Thanks
kumar kasimala.
If you use asp.net you can do like below:
In Web.config file, add
<httpRuntime maxRequestLength="100000" executionTimeout="360"/>
under <system.web>
I googled a bit on your problem and hit upon this page which says that the post size is set by the server in its configuration and can be changed by resetting it - http://forums.sun.com/thread.jspa?threadID=5400480
If you use java:
Use servlet post method to transfer the data to the server
<form method="post" name="sample_form" action="/xxx">
....
</form>
Split it.
Try to split the content to multiple inputs. Your implementation might have problem with that... but that's not too probable
Split the content to multiple requests and send them with AJAX. Collect the responses and be sure to send it in the right order (not all requests at once). Last request should confirm it's the end and load a page returning the pdf

Categories

Resources