0

I add a row to databse with upload image . when i consult the list of rows , the image is appears, but when i try for update a row i encounter the error "Failed to load resource: the server responded with a status of 400 :spring mvc". image not appears!!

//add row 

@RequestMapping(value = "/add")
public String ajouter(@ModelAttribute("serv") Service service ,MultipartFile file) throws Exception {

    Long idser;
    // add
    if (service.getIdService() ==0) {

        service.setImgService(file.getOriginalFilename());
        idser = metier.addservice(service);
        // add new image file
        if (!file.isEmpty()) {
            String path = System.getProperty("java.io.tmpdir") + "/"
                    + idser + "_" + service.getImgService();
            file.transferTo(new File(path));
        }
    }

    return "redirect:/page/pageus";

}

 // update 

 @RequestMapping("/edit/{id}")
    public ModelAndView editService(@PathVariable("id") long id,Model model,@ModelAttribute Service service){

     service=metier.getService(id);
     model.addAttribute("editedserv",service);

     return new ModelAndView("Admin/page/pageedit","serviceObject",service); 
     }


  // get image of the products

    @RequestMapping(value = "Photoser", produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] photoCat(Long idser) throws Exception {
        Service serv = metier.getService(idser);
        String path = System.getProperty("java.io.tmpdir") + "/" + idser+"_"+serv.getImgService();
        File serImage = new File(path);

        return IOUtils.toByteArray(new FileInputStream(serImage));
    }

//show img in jsp
    <img src="Photoser?idser=${serviceObject.idService}"/>

can someone help me !

Raki
  • 1
  • 2
  • 6
  • You should give a image url to a `img` tag's src, not a url retreived byte data. – Blank May 23 '16 at 01:11
  • i save image at tmpdir , so i can't give a image url to a img , i don't find solution ! can help me @Reno !thanx. – Raki May 23 '16 at 14:30

1 Answers1

0

You should use js or jquery to fill src by following content, [your image byte array data] is a string of byte data, so you may need to change your return type to String in controller, anyway try it;)

<img src="data:image/png;base64,[your image byte array data]"/>

Take a look of embedding base64 images.

Community
  • 1
  • 1
Blank
  • 12,308
  • 1
  • 14
  • 32