简体   繁体   English

获取完整的图像路径并使用javascript放入img src

[英]get full image path and put in img src using javascript

I want to get the fullpath of image that i select in , and the problem after i select the image there is no image appear. 我想获取我在中选择的图像的全路径,选择图像后出现的问题是没有图像。

help me please :) 请帮帮我 :)

Here is my code. 这是我的代码。

JAVASCRIPT JAVASCRIPT

<script type="text/javascript">
function imgchange()
{
        var filePath = $('#file').val();
        $("#imgs").attr('src',filePath);
}
</script>

HTML 的HTML

            <form action="#" method="post" enctype="multipart/form-data">
                <center>
                    <table>
                        <tr>
                                <td colspan="2"><center><img id="imgs" width="170px" height="160px" ></img></center></td>
                        </tr>
                        <tr>
                            <td align="right"><b><label for="file">Filename:</label></b></td><td><input type="file" name="file" id="file" onchange="imgchange()"></td>
                        </tr>                               
                    </table>    
                </center>
            <div class="buttons_save">
                <button class="buttons_profile3" id="btnSubmit" >SAVE</button>
            </div>
        </form>

DEMO: - JSFIDDLE 演示: -JSFIDDLE

Try this: 尝试这个:

Here is the HTML code:- 这是HTML代码:-

<input type="file" id="i_file" value=""> 
<input type="button" id="i_submit" value="Submit">
    <br>
<img src="" width="200" style="display:none;" />

Here is the JS:- 这是JS:-

$('#i_file').change( function(event) {
    $("img").fadeIn("fast").attr('src',URL.createObjectURL(event.target.files[0]));
});

Try this http://jsfiddle.net/wTd58/ 试试这个http://jsfiddle.net/wTd58/

HTML 的HTML

<form action="#" method="post" enctype="multipart/form-data">
   <center>
      <table>
         <tr>
            <td colspan="2">
               <center><img id="imgs" width="170px" height="160px" ></img></center>
            </td>
         </tr>
         <tr>
            <td align="right"><b><label for="file">Filename:</label></b></td>
            <td><input type="file" name="file" id="file" onchange="imgchange(this)"></td>
         </tr>
      </table>
   </center>
   <div class="buttons_save">
      <button class="buttons_profile3" id="btnSubmit" >SAVE</button>
   </div>
</form>

Javascript Java脚本

       function imgchange(f) {
           var filePath = $('#file').val();
           var reader = new FileReader();
           reader.onload = function (e) {
               $('#imgs').attr('src',e.target.result);
           };
           reader.readAsDataURL(f.files[0]);           
        }

All browsers may not give you the exact path. 所有浏览器可能都没有给您确切的路径。 You can use blob location like this: 您可以像这样使用Blob位置:

function imgchange(event){ $("#imgs").attr('src',URL.createObjectURL(event.target.files[0])); }

In your input you can use onchange="imgchange(event)" 在您的输入中,您可以使用onchange="imgchange(event)"

Here is a working JSFiddle 这是一个正在工作的JSFiddle

You can't do this unless you have uploaded the file to a server you can't show it in the browser, because the file goes to a virtualpath "C:/fakepath/" and you can't point any image to it. 除非您已将文件上载到服务器,否则无法在浏览器中显示它,否则您将无法执行此操作,因为该文件将进入虚拟路径“ C:/ fakepath /”,并且无法将任何图像指向该文件。 First you have to upload the image to server then provide the server path to the image. 首先,您必须将映像上载到服务器,然后提供映像的服务器路径。

try this: 尝试这个:

 <form action="#" method="post" enctype="multipart/form-data">
                <center>
                    <table>
                        <tr>
                                <td colspan="2"><center><img id="imgs" width="170px" height="160px" ></img></center></td>
                        </tr>
                        <tr>
                            <td align="right"><b><label for="file">Filename:</label></b></td><td><input type="file" name="file" id="file" onchange="imgchange(event)"></td>
                        </tr>                               
                    </table>    
                </center>
            <div class="buttons_save">
                <button class="buttons_profile3" id="btnSubmit" >SAVE</button>
            </div>
        </form>

<script type="text/javascript">
  function imgchange(e)
   {
        $("#imgs").attr('src',URL.createObjectURL(e.target.files[0]));
  }
</script>

Very simple code to display image on browser 非常简单的代码在浏览器上显示图像

function imgchange(event){
    $("#imgs").attr('src',URL.createObjectURL(event.target.files[0]));
}
<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>

</head>
<body>
    <input type='file' onchange="readURL(this);" />
    <img id="blah" src="#" alt="your image" />
</body>
<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(260)
                .height(100);
        };

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

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

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