dt246813579 2012-10-23 05:06 采纳率: 0%
浏览 31
已采纳

使用ajax上传图片

I have the following html form. Here the image upload is working fine. But i want to make the image upload in ajax so that before submitting the form with other details. The preview of the image can be shown in side.

<form action="<?=base_url();?>index.php/insert/insertdata" method="post" enctype="multipart/form-data">

        <tr><td><b>Title: </b></td><td><input type="text" name="title"/></td></tr>
        <tr><td><b>Url: </b></td><td><input type="text" name="url"/></td></tr>

        <tr><td><b>Image: </b></td><td><input type="file" name="image"></td></tr>

        <tr><td><b>Status: </b></td><td><select name="status"><option value="visible">Visible</option><option value="invisible">Invisible</option></select></td></tr>  

        <tr><td><input type="submit" value="submit"/></td><td><input type="reset" value="reset"/></td></tr>
    </form>

jquery is also welcome. But I would like to avoid using the form tag again.

  • 写回答

2条回答 默认 最新

  • duanliang4009 2012-10-23 05:12
    关注

    once your uploading a image then fetch the image name with url and add to the

    $.ajax(url,{data},function(res){
    
            //check status if it is 200 then assign the res to the img
           $('#img').attr('src',res);
    
    })
    

    //Html write this after the image file

     <img src="" id="img">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?