在使用jQuery File Upload插件上传之前获取上传文件名

在使用jQuery File Upload插件上传之前获取上传文件名

问题描述:

我正在使用jQuery File Upload插件 https://github.com/blueimp /jQuery-File-Upload/wiki 将多个文件上传到服务器.我将其与自定义Web服务结合使用,该服务会将上传的图像存储在服务器上.但是,除了POST请求正文外,我还需要将要上传的文件的名称传递给Web服务.因此,每次传输文件时,我还需要将文件名传递给Web服务.有人可以告诉我如何获取文件名并随每个上载请求一起发送吗?我基本上需要从该字段中获取它,而无需任何其他输入字段.如果用户选择了10个文件,则需要为队列中的每个文件取一个名称,然后将其与上传请求一起提交.

I'm using the jQuery File Upload plugin https://github.com/blueimp/jQuery-File-Upload/wiki to 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.

谢谢.

好的,这是可行的解决方案:

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};
});