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.

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

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办