开发者

can I use CKEditor in zk(zk WYSIWYG Editor) to upload image?

开发者 https://www.devze.com 2023-03-25 04:01 出处:网络
because use zk upload component to upload a image,then insert the context path of the image to the C开发者_如何转开发KEditor is too complex,

because use zk upload component to upload a image,then insert the context path of the image to the C开发者_如何转开发KEditor is too complex,

and at http://ckeditor.com/demo, you can see that CKEditor can upload image and flash etc,

but in zk, the CKEditor don't have this feature,

is that mean CKEditor in zk can't upload file?


I'm afraid this is not possible with zk.

I wrote a workaround to do this. You have to add a button to your GUI and add this EventListener to the button:

private class onUpload implements EventListener
{
    @Override
    public void onEvent(Event event) throws Exception
    {
        Media media = ((UploadEvent) event).getMedia();

        if (media.getContentType().contains("image"))
        {
            reader.upload(media.getStreamData(), media.getName());

            String description = edDescription.getValue();
            description += "<img alt=\"\" src=\"/" + media.getName() + "\" />";
            edDescription.setValue(description);
        }
        else
        {
            new Messagebox().show(_T("You can only upload images!"), _T("Not an image!"), Messagebox.OK, Messagebox.ERROR);
        }
    }
}

Reader is my class which handles file transfers and is used to write the data to the docroot. In my case the docroot of glassfish 3.1 can be located with the following code. I wrote the method getDocFolder() for ist because it also adds subfolders for each user if they don't already exists.

 File file = new File("../docroot/");

This is the code for the upload method of the reader:

InputStream inputStream = null;

    try
    {
        inputStream = new ByteArrayInputStream(imageStream);
        String filename = getDocFolder()+"/"+imageName;

        File file = new File(filename);
        OutputStream out=new FileOutputStream(file);
        byte buf[]=new byte[1024];
        int len;

        while((len = inputStream.read(buf)) > 0)
            out.write(buf,0,len);

        out.close();
        inputStream.close();
    } 
    catch (Exception ex)
    {
        Logger.getLogger(ImageReader.class.getName()).log(Level.SEVERE, "Error writing image", ex);
    } 
    finally
    {
        try
        {
            inputStream.close();
        } 
        catch (IOException ex) {}
    }

I hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号