Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取图像以供以后使用_Javascript_Html_Css_Twitter Bootstrap - Fatal编程技术网

Javascript 获取图像以供以后使用

Javascript 获取图像以供以后使用,javascript,html,css,twitter-bootstrap,Javascript,Html,Css,Twitter Bootstrap,我正在建立一个网站,用户可以上传图片,然后它将被显示。稍后我需要将此图像设置为他们的个人资料图片,我对如何设置没有任何线索。非常感谢。这是我的密码: <input id="inp" type='file' accept="image/*"onchange="readURL(this);" /><br> <img id="blah"/> <script type="text/javascript"> function readURL(input)

我正在建立一个网站,用户可以上传图片,然后它将被显示。稍后我需要将此图像设置为他们的个人资料图片,我对如何设置没有任何线索。非常感谢。这是我的密码:

<input id="inp" type='file' accept="image/*"onchange="readURL(this);" /><br>
<img id="blah"/>

<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
        $('#blah')
            .attr('src', e.target.result)
            .width(150)
            .height(150);
    };

    reader.readAsDataURL(input.files[0]);
}
}
</script>


我真的不明白你的问题,你可以将数据保存在一个变量中,然后在你必须发布图像时使用该变量,你应该避免将图像直接存储在数据库中,而是存储对它们的引用

但是,如果您只想在选择图像后显示图像,可以执行以下操作:

let input=$('input'),
img=$('img'),
reader=newfilereader();
函数读取文件(e){
reader.onload=函数(e){
img.attr('src',e.target.result)
}
reader.readAsDataURL(e.target.files[0])
}
input.on('change',readFile)

我想您会希望保留该图像设置吗?这意味着您必须将图像上载到服务器,以便在后续请求中显示图像。