Javascript jqueryajax-使用FormData上传文件
我试图在WordPress管理中使用AJAX上传文件。这是我的密码:Javascript jqueryajax-使用FormData上传文件,javascript,php,jquery,ajax,upload,Javascript,Php,Jquery,Ajax,Upload,我试图在WordPress管理中使用AJAX上传文件。这是我的密码: <script> $(document).ready(function() { $("#upload_app").click(function(e) { event.stopPropagation(); even
<script>
$(document).ready(function()
{
$("#upload_app").click(function(e)
{
event.stopPropagation();
event.preventDefault();
var data = new FormData();
aaa = document.getElementById("path_to_file");
alert(aaa.files[0]);
data.append("newFile",aaa.files[0]);
alert(data["newFile"]);
$.ajax({
url: "'.get_site_url().'/wp-content/plugins/move_and_match/upload_file.php",
type: "POST",
data: data,
cache: false,
dataType: "json",
processData: false,
contentType: false,
success: uploadDone,
error: uploadFailed
});
});
});
function uploadDone(returns)
{
alert(returns);
$("#uploadFileMetaBox p").text("Successfull.");
}
function uploadFailed(returns)
{
alert(returns);
$("#uploadFileMetaBox p").text("Problem occured.");
}
</script>
$(文档).ready(函数()
{
$(“#上传应用程序”)。单击(功能(e)
{
event.stopPropagation();
event.preventDefault();
var data=new FormData();
aaa=document.getElementById(“路径到文件”);
警报(aaa.files[0]);
data.append(“newFile”,aaa.files[0]);
警报(数据[“新文件”]);
$.ajax({
url:“'.get_site_url()./wp content/plugins/move_and_match/upload_file.php”,
类型:“POST”,
数据:数据,
cache:false,
数据类型:“json”,
processData:false,
contentType:false,
成功:上传完成,
错误:上载失败
});
});
});
函数上传完成(返回)
{
警报(返回);
$(“#uploadFileMetaboxP”).text(“Successfull”);
}
函数上载失败(返回)
{
警报(返回);
$(“#UploadFileMetaboxP”).text(“出现问题”);
}
AJAX访问PHP脚本没有问题,但没有发送任何文件。如您所见,我在脚本中添加了一些警报。第一个警报(aaa.files[0])
可以正常工作,但第二个(当aaa的值应该插入FormData对象时)表示该值是未定义的
这是
我做了什么坏事
谢谢你的回答。哦,我知道了,你想用javascript(AJAX)发送文件吗 因为如果安全性不好,您就无法访问通过javascript浏览按钮添加的文件和文件路径。我知道你应该用表格寄 我知道的方法是使用隐藏的
:
元素$(document).ready(function(){
function ajaxUpload(mf){
$(mf).prop('target',#hiddenIframe');
$(mf).submit();
$('#hiddenIframe').on('load',function(){
IframeReady();
}
}
function getFrameContents(iFrame){
var iFrameBody;
if ( iFrame.contentDocument )
{ // FF
iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0];
}
else if ( iFrame.contentWindow )
{ // IE
iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0];
}
return iFrameBody.innerHTML;
}
function IframeReady(){
var miframe=$('#hiddenIframe');
var result;
var $odiv=$('#result');
result=getFrameContents(miframe);
$odiv.html(result);
}
});
HTML:
结果将显示在这里。
请上传您的图片:
PHP文件(saveImage_ajax.PHP)(或任何您想要的文件)
无标题文件
对象不公开其任何项,因此没有FormData
李>数据[“newFile”]
- 你的url完全不可靠<代码>'.get_site_url()。/wp content/plugins/move_and_match/upload_file.php可能不是我们想要的url
url: "<?php echo get_site_url() ?>/wp-content/plugins/move_and_match/upload_file.php",
url:“/wp content/plugins/move_and_match/upload_file.php”,
- 当事件参数为
时使用e
将在未设置全局event
对象的浏览器上失效。您应该将其重命名为event<代码>功能(事件){event
<?php
// do some stuff to save Image In a location.
$ret= json_encode(array('status'=>'ok','src'=>'the destination of image file'));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
echo $ret;
?>
</body>
</html>
url: "<?php echo get_site_url() ?>/wp-content/plugins/move_and_match/upload_file.php",