Php ajaxfileupload与CodeIgniter存在问题
我正在异步上传一个图像,为此我得到了。我阅读了所有内容,并试图删除我不需要的模型,并根据我的需要进行制作,但当我运行时,没有出现错误,文件也没有上传 我在controllerupload函数中放了一个echo,但是我也没有得到echo字符串,即使我在ajaxfileupload中放了完整的路径 以下是我的看法:Php ajaxfileupload与CodeIgniter存在问题,php,codeigniter,Php,Codeigniter,我正在异步上传一个图像,为此我得到了。我阅读了所有内容,并试图删除我不需要的模型,并根据我的需要进行制作,但当我运行时,没有出现错误,文件也没有上传 我在controllerupload函数中放了一个echo,但是我也没有得到echo字符串,即使我在ajaxfileupload中放了完整的路径 以下是我的看法: <!doctype html> <html> <head> <script src="https://ajax.goog
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="<?php echo site_url()?>public/assets/js/ajaxfileupload.js"></script>
<script>
$(function() {
$('#upload_file').submit(function(e) {
e.preventDefault();
$.ajaxFileUpload({
url : 'upload2/upload_file',
secureuri : false,
fileElementId: 'userfile',
dataType : 'json',
data : {
'title': $('#title').val()
},
success : function (data, status)
{
if(data.status != 'error')
{
$('#files').html('<p>Reloading files...</p>');
refresh_files();
$('#title').val('');
}
alert(data.msg);
}
});
return false;
});
});
</script>
</head>
<body>
<h1>Upload File</h1>
<form method="post" action="" id="upload_file">
<label for="title">Title</label>
<input type="text" name="title" id="title" value="" />
<label for="userfile">File</label>
<input type="file" name="userfile" id="userfile" size="20" />
<input type="submit" name="submit" id="submit" />
</form>
<h2>Files</h2>
<div id="files"></div>
</body>
</html>
将此属性添加到表单enctype=“multipart/form data”
无需任何插件或外部脚本即可完成此操作。您需要文件上传元素、一个用于返回数据的iFrame和一个简单的Ajax脚本
文件上载元素:
echo '<input type="file" name="prodImageUpload"
id="prodImageUpload" onchange="return ajaxFileUpload(this);"/>'
在我的控制器中,我有一个自定义上传,但这并不重要。您只需将控件名和最终路径传递给上传。如果有任何错误,我会回显,如果可以,回显文件名和大小,然后打印到iFrame。它非常简单,工作也很好。我认为您需要提供完整的URL。你不能像这样访问我甚至给出了完整的url,但没有运气url可能不完整,但必须以斜杠/开头。。你有收到404或其他http_代码的请求吗?检查Firebug中的响应。没有,我没有收到404错误,浏览器显示了处理过程,但什么都没有发生,furebug中的控制台没有显示任何内容,甚至没有发送请求。我尝试了你的方法,我将输入类型文件和iframe放入表单中,并将js脚本也放入其中,但是我得到了这个错误类型error:upload_field.form.submit不是函数[Break On this error]upload_field.form.submit();它们在表单中吗?这只是控件本身,它仍然需要位于带有enctype=“multipart/form data”的表单中是的,它们位于表单中,并且它具有encrypt=“multipart/form data”图标,并且您的表单字段与我的表单字段类似,带有return onchange=“return ajaxFileUpload(此)?如果您完全掌握了我上面的内容,我没有理由认为它会返回该错误。您没有在此处放置半列(;)
,但脚本已加载,我可以在firebug中看到这一点
echo '<input type="file" name="prodImageUpload"
id="prodImageUpload" onchange="return ajaxFileUpload(this);"/>'
<iframe name="upload_iframe" id="upload_iframe" style="display:block;width:720px;"></iframe>
function ajaxFileUpload(upload_field)
{
var re_text = /\.jpg|\.gif|\.jpeg|\.png/i;
var filename = upload_field.value;
var imagename = filename.replace("C:\\fakepath\\","");
if (filename.search(re_text) == -1)
{
alert("File must be an image");
upload_field.form.reset();
return false;
}
upload_field.form.action = "addProductImage";
upload_field.form.target = "upload_iframe";
upload_field.form.submit();
upload_field.form.action = "";
upload_field.form.target = "";
return true;
}