douzepao0281 2015-10-24 01:46
浏览 18
已采纳

jQuery中的文件上传问题

here is an upload button, from which user can choose the file

Basically what I want to do is whenever user chooses an image from that, it should display it at the place where that "trees" image is.

I'm stuck, really need some help.

here is the code

<div class="user-editable-div">   
        <div class="image-div" id="image_div"><img src="images/image.jpg" id="bg_image" /></div>
        <span class="upload-btn">UPLOAD IMAGE</span>
        <input type="file" id="uploader" />
        <div class="content-div">
            <h1 contenteditable="true">USER EDITABLE</h1>
            <p contenteditable="true">All elements on this page are user editable. You can edit them by simply clicking or by clicking on the edit button next to the element.</p>
    </div>
</div>

when the user chooses a file from "#uploader" it should display the image in "#bg_image"

</div>
  • 写回答

2条回答 默认 最新

  • douzhouqin6223 2015-10-24 05:11
    关注

    With this code when you try to upload image with use file controller at that time that image preview in bg_image element

       <div class="user-editable-div">  
            <div class="image-div" id="image_div"><img src="images/image.jpg" id="bg_image" /></div>
            <span class="upload-btn">UPLOAD IMAGE</span>
            <input type="file" id="uploader" onchange="readURL(this,this.id);"/>
            <div class="content-div">
                <h1 contenteditable="true">USER EDITABLE</h1>
                <p contenteditable="true">
                  All elements on this page are user editable. You can edit them by simply clicking or by clicking on the edit button next to the element.
                </p>
            </div>
        </div>
    
       <script>
    function readURL(input,ids)
    {
        if (input.files && input.files[0])
            {
                var reader = new FileReader();
                reader.onload = function (e)
                {
                    $('#bg_image').attr('src', e.target.result).width(650).height(375);
                };
                reader.readAsDataURL(input.files[0]);
            }
    }       
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c#里的静态变量为什么是错误的?
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误