已从'上传文件;文件'img字段中未显示类型输入字段

Uploaded file from 'file' type input field is not showing in img field

本文关键字:字段 文件 显示 类型 输入 已从 img      更新时间:2024-02-21

我无法使用javascript更改文件上传时显示的图像。这是我的代码:

<html>
<head>
    <title> File Upload </title>
    <script>
        function uploadFile() {
            document.getElementById('picturefield').src='file:///'+document.frmaddstudent.picturefile.src;
            //document.getElementById('picturefield').src='http://icons.iconarchive.com/icons/custom-icon-design/pretty-office-2/128/man-icon.png';
        }
    </script>
</head>
<body>
    <b>Image</b>
    <form name="frmaddstudent">
        <img id="picturefield" src="file:///C:/Users/Nasim/Desktop/myimage.jpg">
        <input type="file" name="picturefile" onchange="uploadFile()">
    </form>
</body>
</html>

但从我的电脑上传文件时,图像没有改变document.frmaddstudent.picturefile.src显示了我使用firebug调试时的空字符串

如果我使用document.frmaddstudent.picturefile.value而不是CCD_ 3,则该值仅显示所选的文件名,但我需要完整的文件路径设置为imgsrc

有人能告诉我我在这里做错了什么吗?Javascript/jQuery中有什么简单的解决方案可以做到这一点吗?

出于安全原因,现代浏览器不支持input file的此功能。

您必须上传该文件,然后在img 中显示