我们都知道单独使用 jquery 是不可能完成文件上传的。但可以使用 jquery 和隐藏的 IFrame 来解决这个问题。
我已经使用这种方法找到了四种解决方案,但不知道如何实现它们。
这个解决方案可以在 Stackoverflow 上找到 https://stackoverflow.com/questions/2909442/how-to-make-asynchronousajax-file-upload-using-iframe,是一种方法。但我不确定这是否是最好的方法。 (未测试)
使用 jQuery 表单插件 http://malsup.com/jquery/form/#file-upload是另一种选择。我尝试以下这个例子 http://www.web-development-blog.com/archives/jquery-form-plugin-and-php-file-uploads/,但这并没有帮助。该解决方案在新页面中加载我的 uploader.php,但无法获取文件信息。我无法使用 IFrame 看到它。
Ajax 文件上传 http://www.phpletter.com/Our-Projects/AjaxFileUpload/是另一种解决方案 - 这是创建一个动态 IFrame。看着示例代码,我不知道如何实现它。
最后的解决方案是AJAX文件上传 http://www.webtoolkit.info/ajax-file-upload.html来自网络工具包。在这里我不知道应该在哪里声明它应该加载哪个 PHP 文件以进行文件处理。
有人有使用这些方法之一的工作示例吗?
我在另一个解决方案中使用了 Uploadify - 但我现在不想使用 flash。
对于#3 这基本上就在他们的网站上。
我是一个 .Net 人员,所以我无法真正帮助您了解接收文件所需的 .php 处理程序,但我希望您发现这很有用。
<html>
<head>
<link href="http://www.phpletter.com/css/general.css" rel="stylesheet" type="text/css" media="screen">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://www.phpletter.com/contents/ajaxfileupload/ajaxfileupload.js"></script>
<script type="text/javascript">
function ajaxFileUpload()
{
$.ajaxFileUpload
(
{
//YOUR URL TO RECEIVE THE FILE
url: 'http://localhost/testing/postfile.php',
secureuri:false,
fileElementId:'fileToUpload',
dataType: 'json',
success: function (data, status)
{
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
alert(data.msg);
}
}
},
error: function (data, status, e)
{
alert(data.error);
alert(e);
}
}
)
return false;
}
</script>
</head>
<body>
<form name="form" action="" method="POST" enctype="multipart/form-data">
<table cellpadding="0" cellspacing="0" class="tableForm">
<thead>
<tr>
<th>Ajax File Upload</th>
</tr>
</thead>
<tbody>
<tr>
<td><input id="fileToUpload" type="file" size="45" name="fileToUpload" class="input"></td>
</tr>
<tr>
<td>Please select a file and click Upload button</td>
</tr>
</tbody>
<tfoot>
<tr>
<td><button class="button" id="buttonUpload" onclick="return ajaxFileUpload();">Upload</button></td>
</tr>
</tfoot>
</table>
</form>
</body>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)