Hi All,

How to Upload a file(.doc ) using Fileupload control and store it as a image file in database. please can anyone help me solving this.


you mean uploading a word document and saving it as an image file?

Yes , Not only word document i have to store .pdf file in database as image . is that possible?

i think it is possible .u can use image column to store images ,if u are using sql server.

ya i am using sql server. But what is the source code to send the pdf file to database and store it has a image file? I am not getting it anywhere? please do help.

Hi all
I'm new here. I also have same problem as Rashmi. I can upload image from client and store that image in database in longblob data column. I could upload .txt or .doc file also.
The problem is when i try to retrieve the uploaded .doc file. The page shows me funny characters. For .txt file, it seems okie no problem in retrieving it.
Is it because i didn't upload word file correctly or my code for downloading word file is wrong.

Thanks for any help..


I found it how to do it already..:D

then y dont u post u r solution,it will be helpfull for those having same problem

i didnt post it cos my code is just testing one. haven't finalized..

<%@ page import = "java.sql.*" %>
<%@ page import = "java.io.*" %>
//include file to connect to database
<%@ include file="....." %>

		String name="test";
       //path of file that would like to upload. the file must be on server
      //for those who would like to upload file directly from client, store client file temporarily at server use that temporary file to upload to database and delete back the tmp file
		String img="/../../../Upload/test2003.doc";
		 String sq1="insert into testUpload (name, size, type, image ) values (?,?,?,?)";
		 int count=0;
		// JFileChooser chooser = new JFileChooser();

         //String imgLocation=img.replace('\\','/');
        File f=new File(img);
			out.println("Test File"+name);
        String[] split_string = name.split("\\.");
		String FileExtension = split_string[1];
   out.println("<br>Test File ext"+FileExtension);
   if(FileExtension.equalsIgnoreCase("doc") || FileExtension.equalsIgnoreCase("txt") || FileExtension.equalsIgnoreCase("docx"))
   else if(FileExtension.equalsIgnoreCase("pdf"))
   fileTypeName = new MimetypesFileTypeMap().getContentType(f);
       out.println("<br>Test File type"+fileTypeName);
        FileInputStream fis=new FileInputStream(f);
		 PreparedStatement pstat = con.prepareStatement(sq1);
		out.println("<br>Uploaded successfully." +count);

 } catch(Exception e){
//end of uploading file to database

//start of retrieve file from database
<%@ page import = "java.sql.*" %>
<%@ page import = "java.io.*" %>
<%@ include file="../connect_db.jsp" %>
	String query="";
	int id =0;
	id      = //it is the id of file tat want to retrieve it back
if(id != 0)

	query   = "SELECT * FROM testUpload WHERE id ="+id;
	Statement stmt=con.createStatement();
        ResultSet rs;
	   rs = stmt.executeQuery(query);
	 byte[] bytearray=new byte[8000];
        int size=0;
        InputStream sImage;
        sImage = rs.getBinaryStream("image");
		response.setHeader("Content-Disposition: attachment","filename="+rs.getString("name"));
	response.setHeader("Content-length:", Integer.toString(rs.getInt("size")));
	response.setContentType( rs.getString("type") );
        while ((size=sImage.read(bytearray)) != -1)



My code is quite messy...hope it can help who r seeking for uploading file to database.

Also correct me if i post anything wrong..tis is my 1st forum :)

HI all,

I got the code to upload the file to database in binary format..

protected void cmdUpload_Click(object sender, EventArgs e)

if (FileUpload1.HasFile == false)
// No file uploaded!
lblUpload.Text = "Please first select a file to upload...";
else if (FileUpload1.PostedFile.ContentType != "application/pdf")

lblUpload.Text = "Upload PDF file only...";
// Display the uploaded file's details
lblUpload.Text = string.Format
@"Uploaded file: "+ FileUpload1.FileName );

Stream imgStream = FileUpload1.PostedFile.InputStream;
int imgLen = FileUpload1.PostedFile.ContentLength;
string imgContentType = FileUpload1.PostedFile.ContentType;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData, 0, imgLen);
int idis = 1;

int RowsAffected = SaveToDB(idis, imgBinaryData);
if (RowsAffected > 0)
Response.Write("<BR>The Image was saved");
Response.Write("<BR>An error occurred uploading the image");


private int SaveToDB(int busid, byte[] imgbin)

SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.AppSettings["CatalogueIndex"];
SqlCommand command = new SqlCommand("INSERT INTO FileNotings (Record_id,Notings) VALUES (@img_busid,@img_contenttype)", con);

SqlParameter param0 = new SqlParameter("@img_busid", SqlDbType.Int, 4);
param0.Value = busid;

SqlParameter param1 = new SqlParameter("@img_contenttype", SqlDbType.Image);
param1.Value = imgbin;

int numRowsAffected = command.ExecuteNonQuery();
return numRowsAffected;