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

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

 错误语法错误:JSON.parse:第1行第1列的意外字符 JSON数据
</ code> </ pre>

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

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

原始表单如下 </ p>

哪个贴到这个php (目前还不是很多)</ p>

这是主要的应用程序脚本JS文件 </ p>
</ div>

展开原文

原文

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

donglikuang8145
donglikuang8145 我不认为pastebin太可能消失,多年来一直非常可靠。但是,如果我们得到一个解决方案,我会倾向于花时间削减和巩固它在这里易于使用。
5 年多之前 回复
douweng1935
douweng1935 toolong
5 年多之前 回复
douxiang3978
douxiang3978 通常我会相信这里有太多代码可供选择。因为这个原因,我设置的pastebin链接不会过期。
5 年多之前 回复
dpjs2005
dpjs2005 您应该在此发布任何编辑和代码,以便将来保留给SO的未来访问者。
5 年多之前 回复
drslez4322
drslez4322 我的编辑有帮助吗?
5 年多之前 回复
douwo1862
douwo1862 toolong
5 年多之前 回复

1个回答

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);
dqj29136
dqj29136 我也试图用PDO做一切PHP ...这对我来说也是一个新的。 :/
5 年多之前 回复
dongxiaoguang9108
dongxiaoguang9108 嗨Riggs,是的,我从你之前的答案得到的部分,我只是不知道如何在php中构建它,文件[]一次只能上传一次或多次,将其作为blob放入数据库中, 每个文件的条目。
5 年多之前 回复
dongsi8812
dongsi8812 查看UploadHandler类并找到运行以保存到磁盘的方法,然后将该函数子类化,使其成为您希望它执行的操作。
5 年多之前 回复
drkbpwk609294
drkbpwk609294 文档有点有用,但如果他们暗示如何将文件本身放入数据库,而不仅仅是文件信息,那肯定会很好。
5 年多之前 回复
douxueke5653
douxueke5653 试试这里的一些DOCS github.com/blueimp/jQuery-File-Upload/wiki,特别是如何提交其他表单数据
5 年多之前 回复
duanfuchi7236
duanfuchi7236 这有很大帮助。 我甚至可以弄清楚你向我展示的内容足以修改它以添加隐藏的帖子数据中包含的所有变量并将其正确输入到我的数据库表中。 非常感谢非常有用和明确的解释,对于像我这样不太熟练的php人。 :)
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐