Home Backend Development PHP Tutorial About the problem of uploading pictures in simditor

About the problem of uploading pictures in simditor

Mar 24, 2017 pm 02:10 PM
upload image

We can customize the toolbar buttons to make simditor richer and realize the function of uploading pictures

Initialize the editor

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<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>

Copy after login

upload defaults to false, set it to true or key-value pair to upload pictures, the interface is out, and Background coding is required (Struts2 in this case)

About the problem of uploading pictures in simditor

You need to modify simditor.js before implementing the function. We can use chrome to inspect the element for "local pictures" and find that there is no name attribute

About the problem of uploading pictures in simditor

Open simditor.js and find it

return $input = $('').appendTo($uploadItem) ;In this line,

you can search accept="image/*" to quickly find and add name="fileData"

in the input as follows:

1

2

return _this.input = $(&#39;<input name="fileData" type="file" title="&#39; + Simditor._t(&#39;uploadImage&#39;)

+ &#39;" accept="image/*">&#39;).appendTo($uploadBtn);

Copy after login

Also continue to search accept="image/*" There is another one below, add name="fileData"

ImgUploadAction

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

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;

    

}

Copy after login

Related articles:

How to use Simditor

javascript - simditor Is there a size limit for uploading images?

javascript - simditor failed to upload large image

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles