我无法上传我的文件,就像在几个教程中所描述的那样,我甚至在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(); ?>
基本上只是普通的文件输入字段。我的jquery就像:
//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);
}
});
});
如果我点击该按钮&#34;添加图片&#34;之后我添加了一些图片文件上传,什么都没有......我在某处看过firefox不喜欢contentType: false
,但我不确定......
答案 0 :(得分:0)
好吧,出于明显的安全原因,Javascript无法访问文件系统。
我在这种情况下做的是创建一个隐藏的iframe然后我提交我的表单以此框架为目标。使用Javascript我能够读取iframe html,然后我知道上传的结果。
另一种解决方案是使用javascript访问的隐藏swf(flash),并使用此swf执行所有上传任务。