通过ajax和php上传图像文件
我对我面临的这个问题不熟悉。。我想通过ajax和php上传图像文件 到目前为止,我已经做了以下工作: HTML: 否则,我如何上传图像? 序列化表单还是什么 请尽可能具体,以便我能理解通过ajax和php上传图像文件,php,ajax,image,opencart,Php,Ajax,Image,Opencart,我对我面临的这个问题不熟悉。。我想通过ajax和php上传图像文件 到目前为止,我已经做了以下工作: HTML: 否则,我如何上传图像? 序列化表单还是什么 请尽可能具体,以便我能理解 提前谢谢 好的,我解决了关于我自己问题的问题/错误。实际上,我对openCart如何上传图像、ajax和xhr如何工作等有很多了解。无论如何,我使用的代码是: 内部文件配置文件。tpl: $('#upload-image').on('click',function(e){ var formData = n
提前谢谢 好的,我解决了关于我自己问题的问题/错误。实际上,我对openCart如何上传图像、ajax和xhr如何工作等有很多了解。无论如何,我使用的代码是: 内部文件配置文件。tpl:
$('#upload-image').on('click',function(e){
var formData = new FormData($('#submitProfile')[0]);
formData.append('route','account/profile');
$.ajax({
url: 'index.php?route=tool/upload/logo',
type: 'post',
data: formData,
cache: false,
contentType: false,
processData: false,
beforeSend: function() {
$("#containerAlertMessages").html("<div class='alert alert-info'><strong>Sending...</strong></div>");
$("#containerAlertMessages").removeClass("containerAlertMessages").addClass("containerAlertMessagesVisible");
},
complete: function() {
$("#containerAlertMessages").html("<div id='alertMessages' class='alert alert-success'><strong>Success!</strong> Your logo has been uploaded successfully! Do not forget to save the abovr information by clicking the Save button at the bottom right corner!</div>");
$("#containerAlertMessages").removeClass("containerAlertMessages").addClass("containerAlertMessagesVisible");
},
success: function() {
//nothing yet
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
public function logo() {
$this->load->language('tool/upload');
if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) {
$filename = html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8');
$route = $this->request->post['route'];
if($route == "account/profile") {
$file = "image/" . $filename;
move_uploaded_file($this->request->files['file']['tmp_name'], DIR_LOGOS . $file);
}
}
}
- DIR_logo已在config.php文件中定义,因此如果需要,我可以在另一个文件中再次引用该变量
- 此外,我还发送了route变量,以了解图像上传的来源。 因此,如果上传来自“account/profile”,我会将图像保存到“/image”文件夹中 如果它来自“acoun/register”,我将保存到“/image/register”或任何我想要的文件夹(无论是否存在),依此类推
if (!empty($this->request->files)) {
foreach ($this->request->files as $key => $file) {
if (!empty($file['name']) && is_file($file['tmp_name'])) {
$filename = date('YmdHis') . rand(0,10000) . html_entity_decode($file['name'], ENT_QUOTES, 'UTF-8');
move_uploaded_file($file['tmp_name'], "image/".$filename);
}
}
}
main.js
$('#pForm').on('submit',function (e) {
e.preventDefault();
$.post({
url: 'index.php?route=ajax/index',
data: new FormData(this),
cache:false,
contentType: false,
processData: false,
success: function () {
//To do something
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
})
我相信这是不一样的。。因为我说的是opencart。。。我正在尝试一种不同的系统。。但是,我会感谢一些帮助,我在你的链接阅读。。因为信息太多了。。而你正在失去我。。
public function logo() {
$this->load->language('tool/upload');
if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) {
$filename = html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8');
$route = $this->request->post['route'];
if($route == "account/profile") {
$file = "image/" . $filename;
move_uploaded_file($this->request->files['file']['tmp_name'], DIR_LOGOS . $file);
}
}
}
if (!empty($this->request->files)) {
foreach ($this->request->files as $key => $file) {
if (!empty($file['name']) && is_file($file['tmp_name'])) {
$filename = date('YmdHis') . rand(0,10000) . html_entity_decode($file['name'], ENT_QUOTES, 'UTF-8');
move_uploaded_file($file['tmp_name'], "image/".$filename);
}
}
}
$('#pForm').on('submit',function (e) {
e.preventDefault();
$.post({
url: 'index.php?route=ajax/index',
data: new FormData(this),
cache:false,
contentType: false,
processData: false,
success: function () {
//To do something
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
})