Can I upload an image into a blob by the Servoy Headless Client?
Does anyone have some sample-code?
It is rather urgent!
Can I upload an image into a blob by the Servoy Headless Client?
Does anyone have some sample-code?
It is rather urgent!
Hi Harjo,
You can use the Image plugin to fetch a file and store it in a BLOB.
You can use the File plugin to do the same.
You can also put an editable image_media field on the form and drop the image file in there.
Take your pick
Hope this helps.
Robert, it is for the headless client!
I think the file plugin, won’t work in a browser!
drop the file in a field, in a webbrowser?
I don’t understand.
HJK:
Robert, it is for the headless client!
Oh! My mistake. I thought to read ‘Servoy Client’.
Well lets see.
You can upload files in HTML by using a form like this:
<form action="myImageUploadPage.jsp" method="post" enctype="multipart/form-data">
Image 1: <input type="file" name="file1" value="">
</form>
You can check if JSP can fetch this file1 variable and pass it to a Servoy method via an argument.
Hope this helps.
Hmm..I’ve been doing some testing to get this to work but it’s not that simple.
For one I get NULL back when I try to fetch the file1 value:
Even plain JSP code that is available on the net have the same result.
Which leads me to suspect that there is a limit on the size of what can be send (in one variable) to Servoy’s Tomcat.
I have notice this before when I wanted to pass some large variables from one page to another.
Hmm..can’t get this to work.
The following code is plain JSP that I got from the web.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Upload Test</title>
</head>
<body>
<form action="upload.jsp" name="upform" enctype="multipart/form-data" method="post">
<table width="60%" border="0" cellspacing="1" cellpadding="1" align="center" class="style1">
<tr>
<td align="left">
<b>Select a file to upload :</b>
</td>
</tr>
<tr>
<td align="left">
<input type="file" name="fileName" size="50">
</td>
</tr>
<tr>
<td align="left">
<input type="hidden" name="todo" value="upload"> <input type="submit" name="Submit" value="Upload"> <input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>
And the upload file: upload.jsp
<%@ page import = "java.io.*" %>
<%@ page import = "java.util.*" %>
<%
String path=request.getParameter("fileName");
String newPath="";
int count=0;
if(path!=null)
{
ArrayList arr=new ArrayList();
StringTokenizer st=new StringTokenizer(path,"\\");
while(st.hasMoreTokens())
{
arr.add(count,st.nextToken());
count++;
}
// create ur own path
newPath="/Users/robert"+arr.get(count-1);
int c;
FileInputStream fis=new FileInputStream(path);
FileOutputStream fos=new FileOutputStream(newPath);
while((c=fis.read())!=-1)
{
fos.write((char)c);
}
}
out.println("Thanks for using");
out.println("
");
out.println("
");
out.println("1.File1 Uploaded from :: "+path);
out.println("
");
out.println("
");
out.println("2.Uploaded File1 is Saved in :: "+newPath);
%>
When I run this I always get NULL back from the previous form.
Maybe can any of the resident JSP/Tomcat guru’s shed a light on this?
Do we need to enable some permission in Tomcat’s xml files?
Hi Harjo,
It was a bit of a headache but I found how to do a file upload in JSP.
Make sure you run it under Servoy Server because then the classpaths are set correctly.
Under Developer it will cry out that it can’t find org.apache.commons.fileupload.*
So here goes:
This is the upload form. You can name it whatever you like.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>file upload</title>
</head>
<body>
<form action="upload.jsp" name="upform" enctype="multipart/form-data" method="post">
<table width="60%" border="0" cellspacing="1" cellpadding="1" align="center">
<tr>
<td align="left">
<b>Select a file to upload :</b>
</td>
</tr>
<tr>
<td align="left">
<input type="file" name="fileName" size="50">
</td>
</tr>
<tr>
<td align="left">
<input type="hidden" name="todo" value="upload"> <input type="submit" name="Submit" value="Upload"> <input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>
And the other file “upload.jsp”
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.File" %>
<%
try {
FileUpload fup=new FileUpload();
boolean isMultipart = FileUpload.isMultipartContent(request);
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();
// Parse the request
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
File cfile = new File(item.getName());
// Path relative to <servoy>/server/webapps/ROOT/
// In this case the subdirectory "upload"
// Make sure the directory exists
File tosave = new File(getServletContext().getRealPath("/") +
"upload/",cfile.getName());
item.write(tosave);
}
}
out.println("<p>Done! Thank you for flying with us ;-)</p>");
// now from here you can tell your Servoy solution where to find the file and fetch it from the HD.
} catch (Exception e){
out.println("<p>Error: " + e + "</p>");
}
%>
I placed comments in the code where you can put your headless client code to load the file into Servoy.
Hope this helps.