jQuery / AngularJS - 使用 AJAX 上传文件
jQuery / AngularJS - Upload file with AJAX
请有人回答,一劳永逸,如果可能 上传文件使用AJAX?
我在网上看到一些帖子说 使用 AJAX 上传文件 不可能!
如果可能的话,有人可以提供工作一段AJAX请求的代码吗?
我已经尝试了大约 10 个示例,这些都是我在网上找到的,但没有人在工作。
请不要向我介绍插件。我想了解它的工作原理并自己实现它。
提前致谢!
试试这个,
HTML
<input id="pic "type="file" name="file" onchange="javascript:this.form.submit();">
JS:
$("#pic").change(function() {
var file_data = $('#pic').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data)
alert(form_data);
$.ajax({
url: 'upload.php',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(dat){
alert('it works maybe');
}
});
});
请有人回答,一劳永逸,如果可能 上传文件使用AJAX?
我在网上看到一些帖子说 使用 AJAX 上传文件 不可能!
如果可能的话,有人可以提供工作一段AJAX请求的代码吗?
我已经尝试了大约 10 个示例,这些都是我在网上找到的,但没有人在工作。
请不要向我介绍插件。我想了解它的工作原理并自己实现它。
提前致谢!
试试这个,
HTML
<input id="pic "type="file" name="file" onchange="javascript:this.form.submit();">
JS:
$("#pic").change(function() {
var file_data = $('#pic').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data)
alert(form_data);
$.ajax({
url: 'upload.php',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(dat){
alert('it works maybe');
}
});
});