Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX文件上载失败_Javascript_Html_Ajax_Codeigniter - Fatal编程技术网

Javascript AJAX文件上载失败

Javascript AJAX文件上载失败,javascript,html,ajax,codeigniter,Javascript,Html,Ajax,Codeigniter,我不能像几个教程中描述的那样上传文件,我甚至在stackoverflow上遵循了多个指南,每次我都会遇到ajax错误 我的html表单在codeigniter中,如下所示: <?php echo form_open_multipart($this->uri->uri_string(), 'class="form-horizontal"'); ?> <fieldset> <div class="control-group <?php e

我不能像几个教程中描述的那样上传文件,我甚至在stackoverflow上遵循了多个指南,每次我都会遇到ajax错误

我的html表单在codeigniter中,如下所示:

 <?php echo form_open_multipart($this->uri->uri_string(), 'class="form-horizontal"'); ?>
 <fieldset>

    <div class="control-group <?php echo form_error('gid') ? 'error' : ''; ?>">
    <?php echo form_label('Image Gallery', 'athletes_gid', array('class' => 'control-label')); ?>
    <div class='controls' >

    <input id='athletes_gid' type='file' name='athletes_gid[]' multiple value="<?php echo set_value('athletes_gid[]', isset($athletes['gid']) ? $athletes['gid'] : time()); ?>" /><br/><br/>
    <div id="addimg" class="btn">Add images</div><br/><br/>

    <input type="submit" name="save" class="btn btn-primary" value="<?php echo lang('athletes_action_edit'); ?>"  />

 </fieldset>
 <?php echo form_close(); ?>

如果我在添加一些图片文件后点击“添加图片”按钮,什么都没有。。。我在某个地方读到firefox不喜欢
contentType:false
,但我不确定…

好吧,出于明显的安全原因,Javascript无法访问文件系统

在这种情况下,我要做的是创建一个隐藏的iframe,然后提交针对该框架的表单。使用Javascript,我能够读取iframe html,然后知道上传的结果


另一个解决方案是由javascript访问的隐藏swf(flash),您可以使用该swf执行所有上载任务。

您能否显示执行处理的php脚本。可能是在php脚本中/tmp路径和它存储文件的实际路径可能是一个问题。
//ajax img upload
// Variable to store your files
var files;

// Add events
$('#athletes_gid').on('change', prepareUpload);

// Grab the files and set them to our variable
function prepareUpload(event)
{
   files = event.target.files;
}

//So now you have a FormData object, ready to be sent along with the XMLHttpRequest.
$( "#addimg" ).click(function() { 

   var data = new FormData();
   $.each(files, function(key, value)
   {
      data.append(key, value);
   });

   $.ajax({
       url: "/public/index.php/admin/content/athletes/multiUpload",
       data: {data: JSON.stringify(data), ci_csrf_token: $("input[name=ci_csrf_token]").val()},
       cache: false,
       contentType: false,
       processData: false,
       type: 'POST',
       success: function(data){
          alert(data);
       }
   });
});