简体   繁体   English

使用ajax在codeigniter中上传图像

[英]upload image in codeigniter using ajax


I have a problem with the upload of some images.我在上传一些图片时遇到问题。 So...I have the admin side in which I click on the "Add image" button and I begin adding the images.所以...我在管理端单击“添加图像”按钮并开始添加图像。 When I begin adding them the image is not shown so that I can see it.当我开始添加它们时,图像没有显示,所以我可以看到它。 And when I click save the images should save to a certain location.当我点击保存时,图像应该保存到某个位置。 My question is: why can't I see my images after I click the Add image button?我的问题是:为什么在单击“添加图像”按钮后看不到我的图像? and why aren't the pictures being saved in the path that I specify?为什么图片没有保存在我指定的路径中? Thanks a lot!非常感谢! I also added some code here:我还在这里添加了一些代码:
This is in my controller:这是在我的控制器中:

 public function showcase_image(){ try{ $config['upload_path'] = './resources/media/showcase/image/'; $config['allowed_types'] = 'jpeg|png|jpg|flv|mp3|wav'; $config['max_size'] = '10000'; $this->load->library('upload', $config); $this->upload->do_upload('add_image'); $data = $this->upload->data(); $w = 720; $h = 425; $this->load->library('image_lib'); $config['image_library'] = 'gd2'; $config['source_image'] = $data['full_path']; $config['create_thumb'] = TRUE; $config['maintain_ratio'] = TRUE; $config['thumb_marker'] = ''; $config['width'] = $w; $config['height'] = $h; $this->image_lib->initialize($config); if($data['image_width']<425 && $data['image_height']<425){ }else{ $this->image_lib->resize(); } $file = $data['file_name']; echo json_encode(array("error"=>false,"msg"=>"success","file"=>$file,"dir"=>"image")); }catch(Exception $e) { echo json_encode(array("error"=>true,"msg"=>$e->getMessage())); } }
This is in my model:

public function showcase_image(){ try{ $config['upload_path'] = './resources/media/showcase/image/'; $config['allowed_types'] = 'jpeg|png|jpg|flv|mp3|wav'; $config['max_size'] = '10000'; $this->load->library('upload', $config); $this->upload->do_upload('add_image'); $data = $this->upload->data(); $w = 720; $h = 425; $this->load->library('image_lib'); $config['image_library'] = 'gd2'; $config['source_image'] = $data['full_path']; $config['create_thumb'] = TRUE; $config['maintain_ratio'] = TRUE; $config['thumb_marker'] = ''; $config['width'] = $w; $config['height'] = $h; $this->image_lib->initialize($config); if($data['image_width']<425 && $data['image_height']<425){ }else{ $this->image_lib->resize(); } $file = $data['file_name']; echo json_encode(array("error"=>false,"msg"=>"success","file"=>$file,"dir"=>"image")); }catch(Exception $e) { echo json_encode(array("error"=>true,"msg"=>$e->getMessage())); } }
This is in my model:

public function showcase_image(){ try{ $config['upload_path'] = './resources/media/showcase/image/'; $config['allowed_types'] = 'jpeg|png|jpg|flv|mp3|wav'; $config['max_size'] = '10000'; $this->load->library('upload', $config); $this->upload->do_upload('add_image'); $data = $this->upload->data(); $w = 720; $h = 425; $this->load->library('image_lib'); $config['image_library'] = 'gd2'; $config['source_image'] = $data['full_path']; $config['create_thumb'] = TRUE; $config['maintain_ratio'] = TRUE; $config['thumb_marker'] = ''; $config['width'] = $w; $config['height'] = $h; $this->image_lib->initialize($config); if($data['image_width']<425 && $data['image_height']<425){ }else{ $this->image_lib->resize(); } $file = $data['file_name']; echo json_encode(array("error"=>false,"msg"=>"success","file"=>$file,"dir"=>"image")); }catch(Exception $e) { echo json_encode(array("error"=>true,"msg"=>$e->getMessage())); } }
This is in my model:
function ajaxFileUploadImage(){
    $("#loading")
        .ajaxStart(function(){
            $(this).show();
        })
        .ajaxComplete(function(){
            $(this).hide();
        });
    $.ajaxFileUpload
    (
        {
            url:'<?=site_url('ajaxadmin/showcase_image')?>',
            secureuri:false,
            fileElementId:'add_image',
            dataType: 'json',
            data:{},
            success: function (data, status)
            {
                //jQuery('.thumb_file').attr('src','<?=base_url()?>resources/media/our_work/thumb/'+data.file);
                //jQuery('input[name=thumb]').val(data.file);
                var image = ' \
                <div class="list" style="float:left;position:relative;margin-left:10px;margin-top:10px;"> \
                    <div class="description_img"> \
                        <div class="delete_img"></div> \
                    </div> \
                    <img height="100" src="<?=base_url()?>resources/media/showcase/image/'+data.file+'" style="z-index: 0; position: relative;"/> \
                    <div class="move_arrows"> \
                        <div class="move_on_left"></div> \
                        <div class="move_on_right"></div> \
                    </div> \
                    <input type="hidden" name="image_filename[]" class="image_filename" value="'+data.file+'"/> \
                </div> \
                ';
                jQuery('#showcase_image').append(jQuery(image));
            },
            error: function (data, status, e){
                jQuery('.response_mes').html('<span class="red">* please try again later!</span>');
            }
        }
    )
    return false;
}

And on the view side I have some ajax:在视图方面,我有一些 ajax:

 function ajaxFileUploadImage(){函数 ajaxFileUploadImage(){\n    $("#loading") $("#loading")\n        .ajaxStart(function(){ .ajaxStart(function(){\n            $(this).show(); $(this).show();\n        }) })\n        .ajaxComplete(function(){ .ajaxComplete(函数(){\n            $(this).hide(); $(this).hide();\n        }); });\n    $.ajaxFileUpload $.ajax文件上传\n    ( (\n        { {\n            url:'<?=site_url('ajaxadmin/showcase_image')?>', url:'<?=site_url('ajaxadmin/showcase_image')?>',\n            secureuri:false,安全:假,\n            fileElementId:'add_image', fileElementId:'add_image',\n            dataType: 'json',数据类型:'json',\n            data:{},数据:{},\n            success: function (data, status)成功:功能(数据,状态)\n            { {\n                //jQuery('.thumb_file').attr('src','<?=base_url()?>resources/media/our_work/thumb/'+data.file); //jQuery('.thumb_file').attr('src','<?=base_url()?>resources/media/our_work/thumb/'+data.file);\n                //jQuery('input[name=thumb]').val(data.file); //jQuery('input[name=thumb]').val(data.file);\n                var image = ' \\ var 图像 = ' \\\n                <div class="list" style="float:left;position:relative;margin-left:10px;margin-top:10px;"> \\ <div class="list" style="float:left;position:relative;margin-left:10px;margin-top:10px;"> \\\n                    <div class="description_img"> \\ <div class="description_img"> \\\n                        <div class="delete_img"></div> \\ <div class="delete_img"></div> \\\n                    </div> \\ </div> \\\n                    <img height="100" src="<?=base_url()?>resources/media/showcase/image/'+data.file+'" style="z-index: 0; position: relative;"/> \\ <img height="100" src="<?=base_url()?>resources/media/showcase/image/'+data.file+'" style="z-index: 0; position: relative;"/> \\\n                    <div class="move_arrows"> \\ <div class="move_arrows"> \\\n                        <div class="move_on_left"></div> \\ <div class="move_on_left"></div> \\\n                        <div class="move_on_right"></div> \\ <div class="move_on_right"></div> \\\n                    </div> \\ </div> \\\n                    <input type="hidden" name="image_filename[]" class="image_filename" value="'+data.file+'"/> \\ <input type="hidden" name="image_filename[]" class="image_filename" value="'+data.file+'"/> \\\n                </div> \\ </div> \\\n                '; ';\n                jQuery('#showcase_image').append(jQuery(image)); jQuery('#showcase_image').append(jQuery(image));\n            }, },\n            error: function (data, status, e){错误:函数(数据,状态,e){\n                jQuery('.response_mes').html('<span class="red">* please try again later!</span>'); jQuery('.response_mes').html('<span class="red">*请稍后再试!</span>');\n            } }\n        } }\n    ) )\n    return false;返回假;\n} }\n


I hope this code will be useful and maybe if someone has a soultion would be great.:D我希望这段代码会有用,也许如果有人有灵魂会很棒。:D

example of how to upload image using ajax.如何使用ajax上传图像的示例。 i think this will help you out.我想这会帮助你。

 <script type="text/javascript"> $(document).ready(function(){ $("#upfile1").click(function () { $("#file1").trigger('click'); }); var input = document.querySelector('input[type=file]'); input.onchange = function () { var file_data = $("#file1").prop("files")[0]; // Getting the properties of file from file field var form_data = new FormData(); // Creating object of FormData class form_data.append("file", file_data) var filevalue = $("#file1").val(); console.log(form_data); $.ajax({ url:'index.php/welcome/uploadImage', type:'POST', data : form_data, cache: false, contentType: false, processData: false, success:function( data ) { console.log(data); }, error:function( error ){ console.log(error); } }); } }); <input type="file" id="file1" name="image" value="" accept="image/*" capture style="display:none"/> <img src="http://macgroup.org/blog/wp-content/uploads/2011/10/iphone-camera-icon-150x150.jpg" id="upfile1" style="cursor:pointer" />

I'm assuming you're using the jQuery AjaxFileUpload plugin found here which uses the "iFrame" upload trick.我假设您正在使用此处找到的 jQuery AjaxFileUpload 插件该插件使用“iFrame”上传技巧。 You should try using qqFileUploader from valums which uses pure AJAX to upload if the browser supports it and falls back on iFrame if necessary.你应该尝试使用qqFileUploader从使用纯AJAX valums上传浏览器是否支持它,并在必要时倒在iFrame中。 I believe it is a superior way to upload via AJAX and will be easier to implement.我相信这是通过 AJAX 上传的一种更好的方式,并且更容易实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM