jQuery 使用jQuery File Upload插件在上传前获取上传文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23266040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the uploading file name before upload with jQuery File Upload plugin
提问by cycero
I'm using the jQuery File Upload plugin https://github.com/blueimp/jQuery-File-Upload/wikito upload multiple files to the server. I'll be using it with a custom web service which will store the uploaded images on the server. However, besides of the POST request body I'll also need to pass to the web service the name of the file which is being uploaded. So, with each file transfer I'll also need to pass to the web service the name of the file. Could anybody tell me how can I take the file name and send it with each upload request? I'll basically need to get it from the field, without any additional input field. If a user chose 10 files, I'll need to take the name for each file in the queue and submit it with the upload request.
我正在使用 jQuery 文件上传插件https://github.com/blueimp/jQuery-File-Upload/wiki将多个文件上传到服务器。我会将它与自定义 Web 服务一起使用,该服务会将上传的图像存储在服务器上。但是,除了 POST 请求正文之外,我还需要将正在上传的文件的名称传递给 Web 服务。因此,在每次文件传输时,我还需要将文件名传递给 Web 服务。有人能告诉我如何获取文件名并随每个上传请求一起发送吗?我基本上需要从现场获取它,而不需要任何额外的输入字段。如果用户选择了 10 个文件,我将需要为队列中的每个文件取名称,并与上传请求一起提交。
Thanks.
谢谢。
回答by cycero
Ok, here's the solution which works:
好的,这是有效的解决方案:
// Storing the file name in the queue
var fileName = "";
// On file add assigning the name of that file to the variable to pass to the web service
$('#fileupload').bind('fileuploadadd', function (e, data) {
$.each(data.files, function (index, file) {
fileName = file.name;
});
});
// On file upload submit - assigning the file name value to the form data
$('#fileupload').bind('fileuploadsubmit', function (e, data) {
data.formData = {"file" : fileName};
});
回答by manish nautiyal
var filename = $('input[type=file]').val().split('\').pop();
回答by TechStone
I have no idea of what kind of web service your site used, resume that is ashx, if that you can use
我不知道你的网站使用什么样的网络服务,简历是ashx,如果你可以使用
HttpPostedFile file = context.Request.Files["Filedata"];
to fetch the filename;
获取文件名;
or use jquery
或使用 jquery
$("#yourid").uploadify({
.....
'onSelect': function(e, queueId, fileObj)
{
alert( "FileName:" + fileObj.name);
}
});
// jquery-file-upload
// jquery-文件上传
$('#fileupload').fileupload({
drop: function (e, data) {
$.each(data.files, function (index, file) {
alert('Dropped file: ' + file.name);
});
},
change: function (e, data) {
$.each(data.files, function (index, file) {
alert('Selected file: ' + file.name);
});
}
});
});