2015-02-18 13:18
浏览 301

如何将文件从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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试得到这个,而不是以一种很好的方式上传文件并将它们放在服务器上,而是转向一些将(希望)将它们放入MySQL的php 数据库。 我可以把它发布得很好,但不是返回任何有用的 - 或者成功 - 我得到:

 错误语法错误:JSON.parse:第1行第1列的意外字符 JSON数据

我不知道为什么。 Firebug告诉我它与php的谈话就好了。 我不确定如何提取在php中以“files []”名称发布的文件,以单独识别和处理它们。 我没有找到任何清楚的理解提取已发布数组的方法(我认为这是什么?)。 通过不处理end方法会导致JSON.parse错误吗? 或者之前是否还有其他事情发生?

AJAX的新手,现在很沮丧。 帮助表示赞赏。


哪个贴到这个php (目前还不是很多)


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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

       error_reporting(E_ALL | E_STRICT);
       $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);
    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, 
            /* 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' )";
            return $file;
    } // end of class
    $upload_handler = new MyUploadHandler($site_params);
    解决 无用
    打赏 举报

相关推荐 更多相似问题