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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题