upload image into blob by SHC?

The forum to discuss the Headless version of Servoy. Web, Java and Servlet development questions can all be posted here.

upload image into blob by SHC?

Postby Harjo » Sun Jan 08, 2006 10:26 am

Can I upload an image into a blob by the Servoy Headless Client?

Does anyone have some sample-code?

It is rather urgent! :?
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Postby ROCLASI » Sun Jan 08, 2006 12:47 pm

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 Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Postby Harjo » Sun Jan 08, 2006 12:57 pm

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.
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Postby ROCLASI » Sun Jan 08, 2006 2:19 pm

HJK wrote: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:
Code: Select all
<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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Postby ROCLASI » Sun Jan 08, 2006 3:28 pm

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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Postby ROCLASI » Sun Jan 08, 2006 4:36 pm

Hmm..can't get this to work.
The following code is plain JSP that I got from the web.
Code: Select all
<!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
Code: Select all
<%@ 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("<br>");
   out.println("<br>");
   out.println("1.File1 Uploaded from :: "+path);
   out.println("<br>");
   out.println("<br>");
   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?
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Postby ROCLASI » Mon Jan 09, 2006 2:30 pm

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.
Code: Select all
<!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"

Code: Select all
<%@ 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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium


Return to Servoy Headless Client

Who is online

Users browsing this forum: No registered users and 8 guests

cron