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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀