gpt4 book ai didi

php - 图像文件为空

转载 作者:行者123 更新时间:2023-12-01 06:09:59 27 4
gpt4 key购买 nike

我创建了一个脚本来上传带有文本的图像,但问题是,当我们发布数据并上传图像时,文件为空,我不知道为什么文件为空,任何人都可以帮我获取正确的文件

这是我的ajax脚本

$(document).ready(function(e) {
$("#post").on('submit', (function(e) {
$("#load").show();

var form = this;
var formData = new FormData(this);
alert(formData);
formData.append('post_dt', $("#contentbox").html());

$.ajax({
url : "http://domainname.com/demo/forums/post_forum",
type : "POST",
data : formData,
contentType : false,
cache : false,
processData : false,
success : function(data) {
$("#data_update").prepend($(data).fadeIn('slow'));
$("#contentbox").empty();
form.reset();
},
complete: function() {
$("#load").hide();
}
});
}));
});

这是正在创建的 html 表单

<form method="POST" action="" id="post" enctype="multipart/form-data" onsubmit="return false;">
<ul>
<li>
<i class="fa fa-photo"></i> Upload A Photo / Document
<input type="file" name="image" />
</li>
</ul>
<div id='display'></div>
<div id="contentbox" contenteditable="true" name="post_dt">
</div>

<input type="submit" id="sb_art" class="btn_v2" value="Start Discussion" />
</form>

我在 Controller 中创建的 php 脚本

public function post_forum() {
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 3000;
$this->load->library('upload', $config);

if(!empty($this->input->post('image'))) {
if (!$this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
} else {
$dt = $this->upload->data();
$file = $dt['file_name'];
}
} else {
$file = 'Filename.jpg';
}
$word_dta = $this->input->post('post_dt');
$time_posted = time();
$user_id = $this->basic_model->is_login($this);

$data_upload = array(
'upload_document' => $file,
'discussion' => $word_dta,
'user_id' => $user_id,
'time_posted' => $time_posted,
'status' => 'Posted'
);

$post_id = $this->basic_model->insertRecord($data_upload, 'forums');

$data = array(
'file_name' => $file,
'data' => $word_dta,
'time_posted' => $time_posted,
'post_id' => $post_id,
'name' => $this->basic_model->getUserData(),
'command' => 'Submit Post'
);

$this->load->view('forum/includes/get_data', $data);
}

最佳答案

id作为imageFieldId添加到html文件中。然后尝试

        var file_data = $('#imageFieldId').prop('files')[0];   
var form_data = new FormData();
form_data.append('file', file_data);
//formData.append('post_dt', $("#contentbox").html());

$.ajax({.....

关于php - 图像文件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42760918/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com