is it possible to upload images to database?

View and download documents on various topics e.g. Configuring Ebase Xi under different environmental setups

Moderators: Jon, Steve, Ian, Dave

cagabit
Ebase User
Posts: 11
Joined: Fri Nov 20, 2015 6:10 pm

is it possible to upload images to database?

#1

Postby cagabit » Tue May 03, 2016 7:09 pm

Hi,

Trying to implement classic contacts application and want to give the user the ability of uploading an image. But as far as i can find the only way in ebase is to upload to a directory and save the file name to database.

Which i think an old way and also have some security concerns, like anybody can see the images from the folder uploaded. ( Ok maybe some security can be defined from web-server side but ... )

Tried to locate some widgets to accomplish this task ( which actually beyond my experience ) but no luck.

Is there any recommendation for this ?
0 x

User avatar
Wai
Moderator
Moderator
Posts: 165
Joined: Wed Sep 12, 2007 9:04 am
Location: Sandy, UK
Contact:

#2

Postby Wai » Tue Jun 20, 2017 9:01 am

You can do this with a bit of Java.
This is just some sample old code, but you would be able to adapt this and put it into a JavaScript function:


Code: Select all

package com.ebasetech.custom.classes;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DBStoreBinary {

	/*
	 * This method stores Binary data into a database column
	 * 
	 * @param String dataSource the datasource to connect to
	 * @param String fileName the file name
	 * @param String fileNameUploaded the uploaded file name	
	 * @param String filePath the path to the file
	 * @param String fileType the type of the file
	 * @param String fileReqId the unique request id
	 * @param String fileDate the uploaded date
	 */	
    public String storeBinary(String dataSource, String fileName, String fileNameUploaded, String filePath, String fileType, String fileReqId, String fileDate) throws IOException, SQLException {    	
    	String status = "";
    	
        Connection conn = null;
        Context ctx;
        FileInputStream fis = null;
    	
        try {
			ctx = new InitialContext();
			DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/" + dataSource);
			conn = ds.getConnection();
                
            String sql = "INSERT INTO file_uploads (fileName, fileNameUploaded, filePath, fileType, fileReqId, fileDate, fileBinary) VALUES (?, ?, ?, ?, ?, ?, ?)"; 
            PreparedStatement stmt = conn.prepareStatement(sql); 
            stmt.setString(1, fileName); 
            stmt.setString(2, fileNameUploaded);
            stmt.setString(3, filePath);
            stmt.setString(4, fileType);
            stmt.setString(5, fileReqId);
            stmt.setString(6, fileDate); 
                  
            File image = new File(filePath);
            fis = new FileInputStream(image); 
            stmt.setBinaryStream(7, fis, (int) image.length()); 
            stmt.execute(); 
            
            status = "Successfully stored into the database";            
        } catch (Exception e) {
        	e.printStackTrace();
            status = "Failed to store into the database: " + e;
        } finally { 
        	if (fis != null) { 
			  fis.close(); 
        	} 
        	if (conn != null && !conn.isClosed()) { 
			  conn.close();
        	} 
      	}  
        
    	return status;
    }
}
0 x


Who is online

Users browsing this forum: No registered users and 4 guests