douxiajiao8445 2015-02-18 13:18
浏览 324
已采纳

如何将文件从AJAX jQuery上传系统上传到mysql数据库

I'm trying to get this to instead of uploading files in a nice way and placing them on the server, to talk instead to some php that will (hopefully) place them into a MySQL database. I can get it posting just fine, but instead of returning anything useful - or succeeding - I get:

Error SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I do not know why. Firebug tells me it talks to the php just fine. I'm unsure how to extract the files which are posted under the name of "files[]" in php, to identify and handle them separately. I've not found any clear to understand method of extracting a posted array (I presume this is what it is?). Would this be causing the JSON.parse error, through not handling the end method well? Or is there something else going on before that?

New to AJAX, and now frustrated. Help appreciated.

Originating form looks like this

Which pastes to this php (currently does not a lot, yet)

This is the main application script JS file

  • 写回答

1条回答 默认 最新

  • douxianglu4370 2015-02-18 14:08
    关注

    The way you get jQueryFileUploader to do what you want is to subclass the class that does all the work on the server.

    If you look at the file structure on the server where you installed the FileUploader it looks something like this

    enter image description here

    what gets run by default is the index.php in the php folder, and that instantiates the UploadHandler class like so

    ?php
       error_reporting(E_ALL | E_STRICT);
       require('UploadHandler.php');
       $upload_handler = new UploadHandler();
    

    And it is the code in that class that does all the actual work of putting files on disk etc etc...

    Now to make it do what you want it to rather than its default process, you need to code your own functionality either instead of the default or as well as the default functionality.

    So what you need to do is subclass the default class and override whichever method in the real class does the work you want to alter.

    To change the process of uploading a file, you would add this method to your MyUploadHandler class

    error_reporting(E_ALL | E_STRICT);
    require('UploadHandler.php');
    
    class MyUploadHandler extends UploadHandler
    {
        /*
         * Maintain our database:
         * For each uploaded file, add an entry to the tbl_xxx
         */
        protected function handle_file_upload($uploaded_file, $name, $size, 
                                              $type, $error, $index = null, 
                                              $content_range = null)
        {
            // this does what woudl have automatically been done 
            // before we subclasses anything
            $file = parent::handle_file_upload($uploaded_file, $name, $size, 
                                               $type, $error, $index, 
                                               $content_range);
    
            /* Now we add some extra processing 
               if the file uploaded with no problems
             */
            if ( empty($file->error) ) {
    
                $sql = "INSERT INTO tbl_xxx ( a,b, imgpath)
                        VALUES('x', 'y', '$file->name' )";
    
                $db->query($sql);
    
            }
            return $file;
        }
    } // end of class
    
    $upload_handler = new MyUploadHandler($site_params);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题