0

I am trying to upload image file to a django Image field. All the fields get populated except image.

My model is as follows:

class ImageModel(models.Model):
    image_title = models.CharField(max_length=50, null=False, blank=False)
    uploader = models.CharField(max_length=50, null=False, blank=False)
    image = models.ImageField(upload_to='imgs/', null=True, blank=True)

And here's the Java code snippet for inserting data:

String query = "INSERT INTO testapp_ImageModel (image_title , uploader , image) VALUES(?, ?, ?)";

File file = new File("Image location goes here");
try {
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte [] buffer = new byte[1024];

        for(int readNum; (readNum = fis.read(buffer)) != -1; ){
            bos.write(buffer, 0, readNum);

            System.out.println("Read " + readNum + " bytes...");
        }

        byte [] image = bos.toByteArray();

        PreparedStatement ps = connection.prepareStatement(query);
        ps.setString(1, "Some Title");
        ps.setString(2, "Uploader Name goes here");


        ps.setBytes(3, image);

        ps.executeUpdate();
    }catch (Exception e) {
    e.printStackTrace();
}

I know ImageField in django won't behave as I am expecting here but don't know how to proceed with the issue. Please someone help me.

optimistic_creeper
  • 2,739
  • 3
  • 23
  • 37

0 Answers0