如何使用uploadify提交表单后上传文件?

时间:2022-11-23 17:41:52

Question:

I have a form with a textbox, file browse(uploadify) and a submit button. I am submitting this form via AJAX. When I select a file using file browser, it is automatically uploaded to a folder defined against folder option. Now after submitting form, I want to save data into database. I am able to get other fields data after post but unable to get uploaded files. I want an array of uploaded files in $_POST after submitting form like this:

我有一个带有文本框,文件浏览(uploadify)和提交按钮的表单。我通过AJAX提交此表单。当我使用文件浏览器选择文件时,它会自动上传到针对文件夹选项定义的文件夹。提交表单后,我想将数据保存到数据库中。我可以在发布后获取其他字段数据但无法获取上传的文件。在提交表单之后,我想在$ _POST中上传一些文件:

$_POST( 'fullname'=>'ABC', 'uploaded_files' => array( '/uploads/abc.doc', '/uploads/xyz.doc' ) );

How it is possible ?

怎么可能?


I have following implementation so far.

到目前为止,我有以下实施。

jQuery:

jQuery('.FileUpload').uploadify({
        'uploader'  : '/uploadify/uploadify.swf',
        'script'    : '/uploadify/uploadify.php',
        'cancelImg' : '/uploadify/cancel.png',
        'folder'    : '/uploads',
        'auto'      : true,
        'queueID'   : 'fileQueue',
        'removeCompleted':false
      }); 

HTML:

<form action='save.php' method='POST' enctype='multipart/form-data'>

Name: <input type='text' name='fullname' id='fullname'>

Source File: <input type='file' name='photos' id='photos' class='FileUpload'>
<div id="fileQueue"></div>

<input type='submit' name='submit' id='submit'>

</form>

2 个解决方案

#1


2  

Using javascript you can add some inputs to your form to trace the uploaded files. For example you can put:

使用javascript,您可以向表单添加一些输入以跟踪上载的文件。例如,你可以把:

<input type='hidden' name='files[]' value="FILENAME1">
<input type='hidden' name='files[]' value="FILENAME2">
<input type='hidden' name='files[]' value="FILENAME3">

#2


2  

Remember that uploadify has an onComplete event that can return data back from your 'script' parameter.

请记住,uploadify有一个onComplete事件,可以从'script'参数返回数据。

So, when uploadify passes the file off, you can then have the script return the path to the file storage location, and then retrieve/store it in your form based on what was passed back in the onComplete event. (This would be the response portion of the onComplete callback)

因此,当uploadify将文件关闭时,您可以让脚本返回文件存储位置的路径,然后根据onComplete事件中传回的内容在表单中检索/存储它。 (这将是onComplete回调的响应部分)

From there, you can populate hidden form fields that are them submitted with the form (as AurelioDeRosa has demonstrated) and populated with the response value(s).

从那里,您可以填充与表单一起提交的隐藏表单字段(如AurelioDeRosa所示)并填充响应值。

#1


2  

Using javascript you can add some inputs to your form to trace the uploaded files. For example you can put:

使用javascript,您可以向表单添加一些输入以跟踪上载的文件。例如,你可以把:

<input type='hidden' name='files[]' value="FILENAME1">
<input type='hidden' name='files[]' value="FILENAME2">
<input type='hidden' name='files[]' value="FILENAME3">

#2


2  

Remember that uploadify has an onComplete event that can return data back from your 'script' parameter.

请记住,uploadify有一个onComplete事件,可以从'script'参数返回数据。

So, when uploadify passes the file off, you can then have the script return the path to the file storage location, and then retrieve/store it in your form based on what was passed back in the onComplete event. (This would be the response portion of the onComplete callback)

因此,当uploadify将文件关闭时,您可以让脚本返回文件存储位置的路径,然后根据onComplete事件中传回的内容在表单中检索/存储它。 (这将是onComplete回调的响应部分)

From there, you can populate hidden form fields that are them submitted with the form (as AurelioDeRosa has demonstrated) and populated with the response value(s).

从那里,您可以填充与表单一起提交的隐藏表单字段(如AurelioDeRosa所示)并填充响应值。