Javascript 获取上载文件的名称
我不熟悉AngularJS1和Js。在这里,我上传的文件将保存在我的驱动器以及mongodb中。我想做的是得到上传的文件名,可以很容易地看到这里的附图。请帮我解决这个问题 这是我的控制器Javascript 获取上载文件的名称,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我不熟悉AngularJS1和Js。在这里,我上传的文件将保存在我的驱动器以及mongodb中。我想做的是得到上传的文件名,可以很容易地看到这里的附图。请帮我解决这个问题 这是我的控制器 (function($) { $.fn.ajaxfileupload = function(options) { var settings = { params: {}, action: '', onStart: function() { }, onCo
(function($) {
$.fn.ajaxfileupload = function(options) {
var settings = {
params: {},
action: '',
onStart: function() { },
onComplete: function(response) { },
onCancel: function() { },
validate_extensions : true,
valid_extensions : ['gif','png','jpg','jpeg'],
submit_button : null
};
var uploading_file = false;
if ( options ) {
$.extend( settings, options );
}
// 'this' is a jQuery collection of one or more (hopefully)
// file elements, but doesn't check for this yet
return this.each(function() {
var $element = $(this);
// Skip elements that are already setup. May replace this
// with uninit() later, to allow updating that settings
if($element.data('ajaxUploader-setup') === true) return;
$element.change(function()
{
// since a new image was selected, reset the marker
uploading_file = false;
// only update the file from here if we haven't assigned a submit button
if (settings.submit_button == null)
{
upload_file();
}
});
if (settings.submit_button == null)
{
// do nothing
} else
{
settings.submit_button.click(function(e)
{
// Prevent non-AJAXy submit
e.preventDefault();
// only attempt to upload file if we're not uploading
if (!uploading_file)
{
upload_file();
}
});
}
var upload_file = function()
{
if($element.val() == '') return settings.onCancel.apply($element, [settings.params]);
// make sure extension is valid
var ext = $element.val().split('.').pop().toLowerCase();
if(true == settings.validate_extensions && $.inArray(ext, settings.valid_extensions) == -1)
{
// Pass back to the user
settings.onComplete.apply($element, [{status: false, message: 'The select file type is invalid. File must be ' + settings.valid_extensions.join(', ') + '.'}, settings.params]);
} else
{
uploading_file = true;
// Creates the form, extra inputs and iframe used to
// submit / upload the file
wrapElement($element);
// Call user-supplied (or default) onStart(), setting
// it's this context to the file DOM element
var ret = settings.onStart.apply($element, [settings.params]);
// let onStart have the option to cancel the upload
if(ret !== false)
{
$element.parent('form').submit(function(e) { e.stopPropagation(); }).submit();
} else {
uploading_file = false;
}
}
};
// Mark this element as setup
$element.data('ajaxUploader-setup', true);
/*
// Internal handler that tries to parse the response
// and clean up after ourselves.
*/
var handleResponse = function(loadedFrame, element) {
var response, responseStr = $(loadedFrame).contents().text();
try {
//response = $.parseJSON($.trim(responseStr));
response = JSON.parse(responseStr);
} catch(e) {
response = responseStr;
}
// Tear-down the wrapper form
element.siblings().remove();
element.unwrap();
uploading_file = false;
// Pass back to the user
settings.onComplete.apply(element, [response, settings.params]);
};
/*
// Wraps element in a <form> tag, and inserts hidden inputs for each
// key:value pair in settings.params so they can be sent along with
// the upload. Then, creates an iframe that the whole thing is
// uploaded through.
*/
var wrapElement = function(element) {
// Create an iframe to submit through, using a semi-unique ID
var frame_id = 'ajaxUploader-iframe-' + Math.round(new Date().getTime() / 1000)
$('body').after('<iframe width="0" height="0" style="display:none;" name="'+frame_id+'" id="'+frame_id+'"/>');
$('#'+frame_id).get(0).onload = function() {
handleResponse(this, element);
};
// Wrap it in a form
element.wrap(function() {
return '<form action="' + settings.action + '" method="POST" enctype="multipart/form-data" target="'+frame_id+'" />'
})
// Insert <input type='hidden'>'s for each param
.before(function() {
var key, html = '';
for(key in settings.params) {
var paramVal = settings.params[key];
if (typeof paramVal === 'function') {
paramVal = paramVal();
}
html += '<input type="hidden" name="' + key + '" value="' + paramVal + '" />';
}
return html;
});
}
});
}
})( jQuery )
(函数($){
$.fn.ajaxfileupload=函数(选项){
变量设置={
参数:{},
行动:“”,
onStart:function(){},
onComplete:函数(响应){},
onCancel:function(){},
验证扩展名:true,
有效的扩展名:['gif'、'png'、'jpg'、'jpeg'],
提交按钮:空
};
var\u file=false;
如果(选项){
$.extend(设置、选项);
}
//“this”是一个或多个jQuery集合(希望如此)
//文件元素,但尚未对此进行检查
返回此值。每个(函数(){
var$element=$(此);
//跳过已设置的元素。可以替换此元素
//稍后使用uninit()以允许更新该设置
if($element.data('ajaxUploader-setup')==true)返回;
$element.change(函数()
{
//由于选择了新图像,请重置标记
上传_文件=false;
//只有在没有指定提交按钮的情况下,才从这里更新文件
if(settings.submit_按钮==null)
{
上传_文件();
}
});
if(settings.submit_按钮==null)
{
//无所事事
}否则
{
设置。提交按钮。单击(功能(e)
{
//防止非AJAXy提交
e、 预防默认值();
//只有在我们不上传文件时才尝试上传文件
如果(!上传_文件)
{
上传_文件();
}
});
}
var upload_file=函数()
{
if($element.val()='')返回settings.onCancel.apply($element,[settings.params]);
//确保扩展名有效
var ext=$element.val().split('.').pop().toLowerCase();
if(true==settings.validate_extensions&&$.inArray(ext,settings.valid_extensions)=-1)
{
//传回给用户
settings.onComplete.apply($element,[{状态:false,消息:'选择的文件类型无效。文件必须是'+settings.valid_extensions.join(',')+'.},settings.params]);
}否则
{
上传_文件=true;
//创建用于
//提交/上传文件
包裹($要素);
//调用用户提供的(或默认)onStart(),设置
//这是文件DOM元素的上下文
var ret=settings.onStart.apply($element,[settings.params]);
//让onStart具有取消上载的选项
如果(ret!==false)
{
$element.parent('form').submit(函数(e){e.stopPropagation();}).submit();
}否则{
上传_文件=false;
}
}
};
//将此元素标记为设置
$element.data('ajaxUploader-setup',true);
/*
//尝试解析响应的内部处理程序
//我们自己打扫干净。
*/
var handleResponse=函数(加载的帧,元素){
var response,responsest=$(loadedFrame.contents().text();
试一试{
//response=$.parseJSON($.trim(responsest));
response=JSON.parse(responsest);
}捕获(e){
response=responsest;
}
//拆掉包装纸
元素。同级().remove();
元素。展开();
上传_文件=false;
//传回给用户
settings.onComplete.apply(元素[response,settings.params]);
};
/*
//将元素包装在标记中,并为每个元素插入隐藏的输入
//key:settings.params中的值对,以便将它们与
//上传。然后,创建一个iframe,整个事情都是这样的
//上传至。
*/
var wrapElement=函数(元素){
//使用半唯一的ID创建要提交的iframe
var frame_id='ajaxUploader iframe-'+Math.round(new Date().getTime()/1000)
$('body')。在('')之后;
$('#'+frame_id).get(0).onload=function(){
HandlerResponse(该元素);
};
//把它包起来
wrap(函数(){
返回“”
})
//为每个参数插入
.在(函数()之前{
var键,html='';
用于(输入设置.参数){
var paramVal=settings.params[key];
if(typeof paramVal==='function'){
paramVal=paramVal();
}
html+='';
}
返回html;
});
}
});
}
})(jQuery)
这是我的ajax文件上传功能
文件列表[0]['name']
?对不起,您可以更详细地介绍一下吗?您可以使用文件列表[0]['name']访问它。在上面的图像中,文件列表对象的名称位于第0位index@guradio-你没有写出OP:p lol的确切代码,@更详细。@guradio此评论是对问题的回答文件列表[0]['name']
?对不起,你可以更详细一点吗?你可以使用文件列表[0]['name']访问它。在上面的图像中,文件列表对象的名称位于第0位index@guradio-你没有写出OP:p lol的确切代码,@更详细。@guradio这个评论就是问题的答案
(function($) {
$.fn.ajaxfileupload = function(options) {
var settings = {
params: {},
action: '',
onStart: function() { },
onComplete: function(response) { },
onCancel: function() { },
validate_extensions : true,
valid_extensions : ['gif','png','jpg','jpeg'],
submit_button : null
};
var uploading_file = false;
if ( options ) {
$.extend( settings, options );
}
// 'this' is a jQuery collection of one or more (hopefully)
// file elements, but doesn't check for this yet
return this.each(function() {
var $element = $(this);
// Skip elements that are already setup. May replace this
// with uninit() later, to allow updating that settings
if($element.data('ajaxUploader-setup') === true) return;
$element.change(function()
{
// since a new image was selected, reset the marker
uploading_file = false;
// only update the file from here if we haven't assigned a submit button
if (settings.submit_button == null)
{
upload_file();
}
});
if (settings.submit_button == null)
{
// do nothing
} else
{
settings.submit_button.click(function(e)
{
// Prevent non-AJAXy submit
e.preventDefault();
// only attempt to upload file if we're not uploading
if (!uploading_file)
{
upload_file();
}
});
}
var upload_file = function()
{
if($element.val() == '') return settings.onCancel.apply($element, [settings.params]);
// make sure extension is valid
var ext = $element.val().split('.').pop().toLowerCase();
if(true == settings.validate_extensions && $.inArray(ext, settings.valid_extensions) == -1)
{
// Pass back to the user
settings.onComplete.apply($element, [{status: false, message: 'The select file type is invalid. File must be ' + settings.valid_extensions.join(', ') + '.'}, settings.params]);
} else
{
uploading_file = true;
// Creates the form, extra inputs and iframe used to
// submit / upload the file
wrapElement($element);
// Call user-supplied (or default) onStart(), setting
// it's this context to the file DOM element
var ret = settings.onStart.apply($element, [settings.params]);
// let onStart have the option to cancel the upload
if(ret !== false)
{
$element.parent('form').submit(function(e) { e.stopPropagation(); }).submit();
} else {
uploading_file = false;
}
}
};
// Mark this element as setup
$element.data('ajaxUploader-setup', true);
/*
// Internal handler that tries to parse the response
// and clean up after ourselves.
*/
var handleResponse = function(loadedFrame, element) {
var response, responseStr = $(loadedFrame).contents().text();
try {
//response = $.parseJSON($.trim(responseStr));
response = JSON.parse(responseStr);
} catch(e) {
response = responseStr;
}
// Tear-down the wrapper form
element.siblings().remove();
element.unwrap();
uploading_file = false;
// Pass back to the user
settings.onComplete.apply(element, [response, settings.params]);
};
/*
// Wraps element in a <form> tag, and inserts hidden inputs for each
// key:value pair in settings.params so they can be sent along with
// the upload. Then, creates an iframe that the whole thing is
// uploaded through.
*/
var wrapElement = function(element) {
// Create an iframe to submit through, using a semi-unique ID
var frame_id = 'ajaxUploader-iframe-' + Math.round(new Date().getTime() / 1000)
$('body').after('<iframe width="0" height="0" style="display:none;" name="'+frame_id+'" id="'+frame_id+'"/>');
$('#'+frame_id).get(0).onload = function() {
handleResponse(this, element);
};
// Wrap it in a form
element.wrap(function() {
return '<form action="' + settings.action + '" method="POST" enctype="multipart/form-data" target="'+frame_id+'" />'
})
// Insert <input type='hidden'>'s for each param
.before(function() {
var key, html = '';
for(key in settings.params) {
var paramVal = settings.params[key];
if (typeof paramVal === 'function') {
paramVal = paramVal();
}
html += '<input type="hidden" name="' + key + '" value="' + paramVal + '" />';
}
return html;
});
}
});
}
})( jQuery )