Javascript 如何将图像从codeigniter上传到mysql

Javascript 如何将图像从codeigniter上传到mysql,javascript,php,mysql,codeigniter,file-upload,Javascript,Php,Mysql,Codeigniter,File Upload,我对CodeIgniter是新手。我不知道如何更新数据库中的表单数据。插入和显示数据库中的数据已完成,但无法理解如何更新数据库中的数据 我已经尝试过研究这个,但是数据没有成功上传 这是我的控制器 //npwp $temp1 = explode(".", $_FILES['file_npwp']['name']); $new_name1 = time().'.'.end($temp1); $config1['upload_path'] = APPPATH.'/file_npwp/'; $confi

我对CodeIgniter是新手。我不知道如何更新数据库中的表单数据。插入和显示数据库中的数据已完成,但无法理解如何更新数据库中的数据

我已经尝试过研究这个,但是数据没有成功上传

这是我的控制器

//npwp
$temp1 = explode(".", $_FILES['file_npwp']['name']);
$new_name1 = time().'.'.end($temp1);
$config1['upload_path'] = APPPATH.'/file_npwp/';
$config1['file_name'] = $new_name1;
$config1['overwrite'] = TRUE;
$config1['allowed_types'] = 'jpg|jpeg|png|pdf';
$this->load->library('upload',$config1);
$this->upload->initialize($config1);
if(!$this->upload->do_upload('file_npwp')){
$this->data['error'] = $this->upload->display_errors();

}
$media1 = $this->upload->data('file_npwp');
这是我的型号

$data_service['file_npwp']= $file_lampiran1;
$data_service['file_ktp']= $file_lampiran2;
$data_service['file_po']= $file_lampiran3;
$data_service['file_registrasi']= $file_lampiran4;
$this->db->where('id_service',$this->input->post('id_service'));
$this->db->update('service_sis', $data_service);
这是我的视图

<div class="form-group">
<label for="">File NPWP</label>
<input type="file" name="file_npwp" id="file_npwp" class="form-control">
</div>
<br>
<div id="hasilloadfoto"></div>
<br>
<p>*Pastikan semua form sudah terisi dengan benar</p>
<button id="SaveAccount" class="btn btn-success">Simpan</button>

文件NPWP


*这是一种新的形式

辛潘
在我看来,这是我的Javascript

$( function() {
var $signupForm = $( '#myForm' );
$signupForm.validate({errorElement: 'em'});
$signupForm.formToWizard({
submitButton: 'SaveAccount',
nextBtnName:  'Selanjutnya',
prevBtnName:  'Sebelumnya',
nextBtnClass: 'btn btn-primary btn-flat next',
prevBtnClass: 'btn btn-default btn-flat prev',
buttonTag:    'button',
validateBeforeNext: function(form, step) {
var stepIsValid = true;
var validator = form.validate();
$(':input', step).each( function(index) {
var xy = validator.element(this);
stepIsValid = stepIsValid && (typeof xy == 'undefined' || xy);
});
return stepIsValid;
},
progress: function (i, count) {
$('#progress-complete).width(''+(i/count*100)+'%');
}

});
});

function filePreview(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#hasilloadfoto + img').remove();
$('#hasilloadfoto').after('<img src="'+e.target.result+'" width="450" height="300"/>');
}
reader.readAsDataURL(input.files[0]);
}
}
$(document).ready(function(){
$("#file_npwp").change(functio () {
filePreview(this);
});
$(函数(){
var$signupForm=$(“#myForm”);
$signupForm.validate({errorElement:'em'});
$signupForm.formToWizard({
submitButton:“保存帐户”,
下一个名称:“Selanjutnya”,
Prevbtname:“Sebelumnya”,
NEXTBTTNCLASS:'btn btn primary btn PLANT next',
prevBtnClass:“btn btn默认btn扁平prev”,
按钮标签:“按钮”,
ValidateBeforNext:函数(表单、步骤){
var stepIsValid=true;
var validator=form.validate();
$(':input',step).each(函数(索引){
var xy=validator.element(此);
stepIsValid=stepIsValid&&(xy的类型==“未定义”| | xy);
});
返回步骤有效;
},
进度:功能(i,计数){
$('#进度完成).width(''+(i/count*100)+'%');
}
});
});
函数文件预览(输入){
if(input.files&&input.files[0]){
var reader=new FileReader();
reader.onload=函数(e){
$('#hasilloadfoto+img')。删除();
$('hasilloadfoto')。在('')之后;
}
reader.readAsDataURL(input.files[0]);
}
}
$(文档).ready(函数(){
$(“#文件_npwp”).change(函数(){
文件预览(本);
});

需要更新操作方面的帮助。非常感谢您的帮助。

您只需在数据库中发送照片的名称,然后在模型中添加即可

$this->input->post('databse_column_name')=$variable_name_which_content_file_name

你可以用两种方法

  • 您可以将映像上载到任何云存储(Aws、Azure等),然后更新数据库上的链接
  • 将图像转换为base64并将其更新为数据库中的字符串
  • 我更愿意进行第一步,我有我上传到Azure并更新我的DB的示例

    //这是在Js上,使用ajax,我将发送给controller 函数uploadImage(){

    //var_dump($数据); echo json_编码($data); }

    助手类

    函数uploadImageToS3($tmp、$bucket、$file\u name){

    在控制器端,我刚刚上传了图像,你可以通过上传的图像链接调用你的模型类,并在你的数据库上更新


    快乐编码:)

    $this->input->post('id\u service')=$data\u service;无法运行,因为错误代码:Message:无法在写入上下文中使用方法返回值文件名:models/Muser.phpi可以编辑代码:$this->input->post('id\u service',$data\u service)。这是运行无消息错误,但不上载数据以进入数据库以替换现有数据。我的意思是,如果数据库服务器中以前存在的数据,我想替换它,如果数据不存在,我想用上载文件来填充它。谢谢您的帮助
        var base_url = '<?php echo BASEURL ?>';
        // call ajax to upload image
        //event.preventDefault();
        var file_data = $('#post-image').prop('files')[0];
        var form_data = new FormData();
        form_data.append('uploaded_file', file_data);
        $.ajax({
            url: base_url + "dashboard/uploadImage",
            dataType: 'json',
            cache: false,
            contentType: false,
            processData: false,
            data: form_data,
            type: 'post',
            success: function (aData) {
    
            },
            error: function (data) {
                alert(data);
            }
        });
    
    }
    
        $data = array();
        if (isset($_FILES ['uploaded_file']['name']) && !empty($_FILES ['uploaded_file']['name'])) {
            $userId = $this->input->post("userId");
    
    
            $file_name = $userId . "_" . uniqid() . ".jpg";
            $S3ImgUrl = uploadImageToS3($_FILES ['uploaded_file']['tmp_name'], "app_feed_images", $file_name);
    
    
            $data = array(
                'code' => 555,
                'message' => 'Image has been uploaded successfully',
                'url' => $S3ImgUrl
            );
        } else {
            $data['code'] = -111;
            $msg = "failed to upload image to s3";
            $data ['status'] = $this->failed_string;
        }
    
    $connectionString = "DefaultEndpointsProtocol=https;AccountName=" . azure_bucket_name . ";AccountKey=" . azure_bucket_key;
    $blobClient = MicrosoftAzure\Storage\Blob\BlobRestProxy::createBlobService($connectionString);
    $containerName = "app-image/" . $bucket;
    $containerUrl = "https://hbimg.blob.core.windows.net/";
    
    $content = fopen($tmp, "r");
    $options = new MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions();
    $content_type = $_FILES['uploaded_file']['type'];
    $options->setContentType($content_type);
    $blobClient->createBlockBlob($containerName, $file_name, $content, $options);
    return $containerUrl . $containerName . "/" . $file_name; }