du27271 2011-10-05 22:00
浏览 85
已采纳

Jquery表单插件图片检索

I am using the Jquery Form plugging to try and upload a picture to my mysql DBMS. I use the JqueryForm ajaxForm() call to do this. It calls a php file on my server and that script puts the file into the database. I then attempt to get that file out of the database in the same script. I guess the nuts and bolts of how I do it is irrelevant. I really want to know how I would get a picture back from an ajax call using the AjaxForm call from the jqueryForm pluggin. Does anybody have an example of how to do this using that pluggin? I am a little lost...

<script type="text/javascript" src="jquery.form.js"></script>
<script>
    $(document).ready(function () {
        $('#profilepicbutton').live('change', function () {
            $("#preview").html('');
            $("#preview").html('<img src="loader.gif" alt="Uploading...."/>');
            $("#registerpt3").ajaxForm({
                target: '#preview',
                success: function (data) {
                    $("#preview").html('');
                    $("#preview").append("<img src=" + data + "></img>");
                }
            }).submit();
        });
    });
</script>    

Now, On the jquery form pluggin site, there is a page in particular that has instructions for file uploads...
http://jquery.malsup.com/form/#file-upload The example that they give is a little blank...

<textarea> 
for (var i=0; i < 10; i++) { 
    // do some processing 
} 
</textarea>

Now, what am I supposed to do with that? Why am I looping through some data structure? If you look on there page, you will see they are remarkable brief in their instructions of what to do. Anybody have any tutorials or advice? Thanks.

UPDATE PHP Code

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{   
$name = $_FILES['profilepicinput']['name'];
$size = $_FILES['profilepicinput']['size'];
if(strlen($name))
{
    list($txt, $ext) = explode(".", $name);
    if(in_array($ext,$valid_formats))
    {
        if($size<(1024*1024)) // Image size max 1 MB
        {
            $actual_image_name = time().$session_id.".".$ext;
            $tmp = $_FILES['profilepicinput']['tmp_name'];
            $fp      = fopen($tmp, 'r');
            $data = fread($fp, filesize($tmp));
            $data = addslashes($data);
            fclose($fp);
             values('$email', '$tmp')");

            if(mysql_query("insert into Personal_Photos (Email, Pics) values('$email', '$data')"))
            {
                $query="select Pics, MAX(ID) from Personal_Photos where Email='$email'";
                $result=mysql_query($query) or die("Error: ".mysql_error());
                $row=mysql_fetch_array($result);
                $mime = 'image/yourtype';
                $base64 = base64_encode($contents);
                $uri = "data:$mime;base64,$base64";
                header("Content-type: image/jpg");
                print($row['Pics']);
            }
            else
            {
                die('Invalid query: ' . mysql_error());
                echo "failed";
            }
        }
        else
            echo "Image file size max 1 MB. Image Size:"+$size;
    }
    else
        echo "Invalid file format..";
}
else
    echo "Please select image..! Bull shit".$email;
exit;

}

  • 写回答

1条回答 默认 最新

  • dongsi1954 2011-10-05 22:10
    关注

    If I understand your question right, you can create a so called data URI.

    In PHP it's quite simple:

    $mime = 'image/yourtype';
    $base64 = base64_encode($contents);
    $uri = "data:$mime;base64,$base64";
    

    And pass this as string in the ajax reponse to be directly entered as you outlined in your question.

    Hopefully this helps, I'm not fluent with jqueryform.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊