关于simditor上传图片的问题

PHP中文网
Lepaskan: 2023-03-01 07:06:01
asal
3695 orang telah melayarinya

我们可自定义工具栏按钮使simditor实现更丰富和实现上传图片功能

初始化编辑器

<script type="text/javascript">
   $(function(){
    toolbar = [ &#39;title&#39;, &#39;bold&#39;, &#39;italic&#39;, &#39;underline&#39;, &#39;strikethrough&#39;,
            &#39;color&#39;, &#39;|&#39;, &#39;ol&#39;, &#39;ul&#39;, &#39;blockquote&#39;, &#39;code&#39;, &#39;table&#39;, &#39;|&#39;,
            &#39;link&#39;, &#39;image&#39;, &#39;hr&#39;, &#39;|&#39;, &#39;indent&#39;, &#39;outdent&#39; ];
    var editor = new Simditor( {
        textarea : $(&#39;#editor&#39;),
        placeholder : &#39;这里输入内容...&#39;,
        toolbar : toolbar,  //工具栏
        defaultImage : &#39;simditor-2.0.1/images/image.png&#39;, //编辑器插入图片时使用的默认图片
        upload : {
            url : &#39;ImgUpload.action&#39;, //文件上传的接口地址
            params: null, //键值对,指定文件上传接口的额外参数,上传的时候随文件一起提交
            fileKey: &#39;fileDataFileName&#39;, //服务器端获取文件数据的参数名
            connectionCount: 3,
            leaveConfirm: &#39;正在上传文件&#39;
        } 
    });
   })
</script>
Salin selepas log masuk

upload默认为false,设置为true或者键值对就可以实现上传图片,界面是出来了,还需要进行后台编码(本例为Struts2)

1.png

实现功能之前需要修改一下simditor.js,我们可以对"本地图片" 用chrome审查元素发现没有name属性

2.png

打开simditor.js找到

return $input = $('').appendTo($uploadItem);这一行,

可以搜索accept="image/*" 快速找到在input里加上 name="fileData"

如下:

return _this.input = $(&#39;<input name="fileData" type="file" title="&#39; + Simditor._t(&#39;uploadImage&#39;) 
+ &#39;" accept="image/*">&#39;).appendTo($uploadBtn);
Salin selepas log masuk

同样继续搜索accept="image/*" 下面还有一个,加上name="fileData"

ImgUploadAction

public class ImgUploadAction extends ActionSupport {  
    private static final long serialVersionUID = 1L;
    private String err = "";  
    private String msg;              //返回信息  
    private File fileData;           //上传文件  
    private String fileDataFileName; //文件名  
  
    public String imgUpload() {  
        //获取response、request对象  
        ActionContext ac = ActionContext.getContext();  
        HttpServletResponse response = (HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);  
        HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);  
          
        response.setContentType("text/html;charset=gbk");  
          
        PrintWriter out = null;  
        try {  
            out = response.getWriter();  
        } catch (IOException e1) {  
            e1.printStackTrace();  
        }  
  
        String saveRealFilePath = ServletActionContext.getServletContext().getRealPath("/upload");  
        File fileDir = new File(saveRealFilePath);  
        if (!fileDir.exists()) { //如果不存在 则创建   
            fileDir.mkdirs();  
        }  
        File savefile;  
        savefile = new File(saveRealFilePath + "/" + fileDataFileName); 
        try {  
            FileUtils.copyFile(fileData, savefile);  
        } catch (IOException e) {  
            err = "错误"+e.getMessage();  
            e.printStackTrace();  
        }  
        String file_Name = request.getContextPath() + "/upload/" + fileDataFileName;  
        
        msg = "{\"success\":\"" + true + "\",\"file_path\":\"" + file_Name + "\"}";  
        out.print(msg); //返回msg信息  
        return null;  
    }  
  
    public String getErr() {  
        return err;  
    }  
    public void setErr(String err) {  
        this.err = err;  
    }  
    
    public String getMsg() {  
        return msg;  
    }  
    public void setMsg(String msg) {  
        this.msg = msg;  
    }
    
    public File getFileData() {
        return fileData;
    }
    public void setFileData(File fileData) {
        this.fileData = fileData;
    }
    
    public String getFileDataFileName() {
        return fileDataFileName;
    }
    public void setFileDataFileName(String fileDataFileName) {
        this.fileDataFileName = fileDataFileName;
    }  
}
Salin selepas log masuk

相关文章:

Simditor使用方法

javascript - simditor 上传图片大小有限制吗?

javascript - simditor 上传大图失败

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!