Home php教程 php手册 利用ctrl+v实现粘贴截图完成上传功能

利用ctrl+v实现粘贴截图完成上传功能

Jun 06, 2016 pm 07:38 PM
upload use Function Finish accomplish screenshot Paste

今天发现segmentfault的评论留言里面可以粘贴上传图片,于是研究了下怎么实现的! 原理很简单其实就是监控粘贴事件,然后检测是否粘贴的东西里面有图片,有的话直接触发ajax上传 更多技术文章请访问:http://www.dahuzhi.com 代码可以直接运行,有兴趣你们可以试试

今天发现segmentfault的评论留言里面可以粘贴上传图片,于是研究了下怎么实现的!
原理很简单其实就是监控粘贴事件,然后检测是否粘贴的东西里面有图片,有的话直接触发ajax上传

更多技术文章请访问: http://www.dahuzhi.com

代码可以直接运行,有兴趣你们可以试试 PHP JavaScript Localisation

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

<?php

header("Access-Control-Allow-Origin:*");

$url  = 'http://'.$_SERVER['HTTP_HOST'];

$file = (isset($_POST["file"])) ? $_POST["file"] : '';

if($file)

{

    $data = base64_decode(str_replace('data:image/png;base64,', '', $file));  //截图得到的只能是png格式图片,所以只要处理png就行了

    $name = md5(time()) . '.png'// 这里把文件名做了md5处理

    file_put_contents($name, $data);

    echo "$url/$name";

    die;

}

?>

 

 

<div id="box" style="width:400px;height:400px;border:1px solid;" contenteditable>

</div>

<input type="hidden" name="img" value="" id="img_puth"/>

 

<script>

    //查找box元素,检测当粘贴时候,

    document.querySelector('#box').addEventListener('paste', function(e) {

 

        //判断是否是粘贴图片

        if (e.clipboardData && e.clipboardData.items[0].type.indexOf('image') > -1)

        {

            var that     = this,

                reader   = new FileReader();

                file     = e.clipboardData.items[0].getAsFile();

 

            //ajax上传图片

            reader.onload = function(e)

            {

                var xhr = new XMLHttpRequest(),

                    fd  = new FormData();

 

                xhr.open('POST', '', true);

                xhr.onload = function ()

                {

                    var img = new Image();

                    img.src = xhr.responseText;

 

                    // that.innerHTML = '<img src="/static/imghw/default1.png"  data-src="'+img.src+'"  class="lazy"+img.src+'" alt=""/>';

                    document.getElementById("img_puth").value = img.src;

                }

 

                // this.result得到图片的base64 (可以用作即时显示)

                fd.append('file', this.result);

                that.innerHTML = '<img src="/static/imghw/default1.png"  data-src="'+this.result+'"  class="lazy"+this.result+'" alt=""/>';

                xhr.send(fd);

            }

            reader.readAsDataURL(file);

        }

    }, false);

</script>

Copy after login
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

The difference between vivox100s and x100: performance comparison and function analysis The difference between vivox100s and x100: performance comparison and function analysis Mar 23, 2024 pm 10:27 PM

Both vivox100s and x100 mobile phones are representative models in vivo's mobile phone product line. They respectively represent vivo's high-end technology level in different time periods. Therefore, the two mobile phones have certain differences in design, performance and functions. This article will conduct a detailed comparison between these two mobile phones in terms of performance comparison and function analysis to help consumers better choose the mobile phone that suits them. First, let’s look at the performance comparison between vivox100s and x100. vivox100s is equipped with the latest

What to do if a black screen appears when taking a screenshot on a win10 computer_How to deal with a black screen when taking a screenshot on a win10 computer What to do if a black screen appears when taking a screenshot on a win10 computer_How to deal with a black screen when taking a screenshot on a win10 computer Mar 27, 2024 pm 01:01 PM

1. Press the win key + r key, enter regedit, and click OK. 2. In the opened registry editor window, expand: HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlGraphicsDriversDCI, select Timeout on the right and double-click. 3. Then change 7 in [Numeric Data] to 0, and confirm to exit.

iPhone screenshots not working: How to fix it iPhone screenshots not working: How to fix it May 03, 2024 pm 09:16 PM

Screenshot feature not working on your iPhone? Taking a screenshot is very easy as you just need to hold down the Volume Up button and the Power button at the same time to grab your phone screen. However, there are other ways to capture frames on the device. Fix 1 – Using Assistive Touch Take a screenshot using the Assistive Touch feature. Step 1 – Go to your phone settings. Step 2 – Next, tap to open Accessibility settings. Step 3 – Open Touch settings. Step 4 – Next, open the Assistive Touch settings. Step 5 – Turn on Assistive Touch on your phone. Step 6 – Open “Customize Top Menu” to access it. Step 7 – Now you just need to link any of these functions to your screen capture. So click on the first

How to use shortcut keys to take screenshots in Win8? How to use shortcut keys to take screenshots in Win8? Mar 28, 2024 am 08:33 AM

How to use shortcut keys to take screenshots in Win8? In our daily use of computers, we often need to take screenshots of the content on the screen. For users of Windows 8 system, taking screenshots through shortcut keys is a convenient and efficient operation method. In this article, we will introduce several commonly used shortcut keys to take screenshots in Windows 8 system to help you take screenshots more quickly. The first method is to use the "Win key + PrintScreen key" key combination to perform full

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Simple steps to upload your own music on Kugou Simple steps to upload your own music on Kugou Mar 25, 2024 pm 10:56 PM

1. Open Kugou Music and click on your profile picture. 2. Click the settings icon in the upper right corner. 3. Click [Upload Music Works]. 4. Click [Upload Works]. 5. Select the song and click [Next]. 6. Finally, click [Upload].

What exactly is self-media? What are its main features and functions? What exactly is self-media? What are its main features and functions? Mar 21, 2024 pm 08:21 PM

With the rapid development of the Internet, the concept of self-media has become deeply rooted in people's hearts. So, what exactly is self-media? What are its main features and functions? Next, we will explore these issues one by one. 1. What exactly is self-media? We-media, as the name suggests, means you are the media. It refers to an information carrier through which individuals or teams can independently create, edit, publish and disseminate content through the Internet platform. Different from traditional media, such as newspapers, television, radio, etc., self-media is more interactive and personalized, allowing everyone to become a producer and disseminator of information. 2. What are the main features and functions of self-media? 1. Low threshold: The rise of self-media has lowered the threshold for entering the media industry. Cumbersome equipment and professional teams are no longer needed.

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

See all articles