简体   繁体   English

从Internet复制粘贴图像到输入字段

[英]Copy-Paste image from internet to input field

I'm using this nice plugin which allow to copy and paste an image from a website to my form. 我正在使用这个不错的插件,它允许将图像从网站复制并粘贴到我的表单中。 https://github.com/vladmalik/pasteimage https://github.com/vladmalik/pasteimage

So I can preview the image, but I can't add it to my input field : 因此,我可以预览图像,但不能将其添加到输入字段中:

<input type="file" id="eventPicture" accept="image/*">

The function of the plugin is : 该插件的功能是:

function insertImageURI(value) {
    // parse the uri to strip out "base64"
    var sourceSplit = value.split("base64,");
    var sourceString = sourceSplit[1];
    // Write base64-encoded string into input field
    $("input").val(sourceString);
}

But what I put in the "value" variable ? 但是我在“值”变量中输入了什么? thanks 谢谢

EDIT : my code is : 编辑:我的代码是:

<script>
        function callback(src) {
            //do something e.g., assign src to image
            showImage(src);
            insertImageURI(src); // ERROR
        }

        $(function() {
            $.pasteimage(callback);
        });

        function showImage(src) {
          $('#pasteImage').attr("src", src);
        }

        function insertImageURI(value) {
            // parse the uri to strip out "base64"
            var sourceSplit = value.split("base64,");
            var sourceString = sourceSplit[1];
            // Write base64-encoded string into input field
            $('#eventPicture').val(sourceString);
        }
    </script>

Error : Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string. 错误 :未捕获InvalidStateError:无法在“ HTMLInputElement”上设置“ value”属性:此输入元素接受文件名,该文件名只能以编程方式设置为空字符串。

EDIT 2 : looks like the value property of input:file is read-only so I can't fill it programmatically :( 编辑2:看起来input:file的value属性是只读的,所以我无法以编程方式填充它:(

Two things there 那里有两件事

1.you have to display the image while pasting it in the page level 1.您必须在粘贴到页面级别时显示图像

  1. You need to give your URL of the image in insertImageUri the back end server will fetch it as image and upload 您需要在insertImageUri中提供图像的URL,后端服务器将其作为图像获取并上传

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM