I have found solutions on how to add additional form data when submitting the file upload form. This question is how to upload the additional data if there is no file to upload.
I am using blueimp jquery-file-upload in a task management app in order to drag and drop files and attach them to a task.
The script is initialized and setup to not automatically upload when files are attached. On the fileuploadadd
callback I attach data.submit()
to my submit
event handler. This accomplishes that we submit the task data and the files in one POST request.
Until files are added I'm unable to get access to the file-upload data
to use the data.submit()
function. I came up with a work around by adding an empty file (and then removing it) on page load which would trigger the binding data.submit()
to the submit button. The problem is that the plugin is returning an error while trying to loop through an empty array of files. This problem would also occur if you added a file and then removed it before submitting the form.
I have been looking for a solution to this for a while and have looked high and low but couldn't find anything in the (IMHO) terrible documentation.
Have a look at my code below:
$('#post_task').fileupload({
autoUpload: false,
singleFileUploads: false,
disableImagePreview: true,
}).on('fileuploadadd', function (e, data) {
$.each(data.files, function (index, file) {
var filename = file.name,
filesize = bytesToSize(file.size)
ext = filename.substr(filename.lastIndexOf('.')+1,5),
icon = '<i class="sprite_file sprite_file-file_extension_'+ext+'"></i>',
node = $('<li/>').append($('<span/>').html(icon + filename + ' ' + filesize + '<a href="#">×</a>')).attr('data-index',index);
node.find('a').click(function(e){
e.preventDefault();
var $self = $(this),
$listItem = $self.parents('li'),
listIndex = $listItem.attr('data-index');
$listItem.remove();
$('#files li').attr('data-index',function(index){return index;});
data.files.splice(listIndex,listIndex);
console.log(data);
vardata = data;
});
$('#files').append(node);
});
$('#post_task').unbind('submit').submit(function(ev){
ev.preventDefault();
data.submit();
});
});
I faced the same problem and I ended up adding an empty file prior to submitting if there is no file.
This works perfectly in my situation and the backend receives the POST while the submitted file is null.
With most browsers (tested Chrome and FF) my Backend will receive no file (null) but with IE8 there is one with size 0. I haven't tested it with any other IE.
@Hirshy and @Luk, your solution is really quite elegant and works like a charm. The files input field does not even get sent to the server so it's easy to determine when a file is in the payload.
In my Angular app, I have a single view for both adding a new document and some attendant data and for editing the data and/or uploading a replacement document.
Here is my solution:
UPDATE
It turns out
uploadForm.fileupload('add', { files: [''] });
will result in an exception being thrown in the browser if the server returns a failed status. JFU tries to assigndata.files[0].error
and data.files[0] doesn't exist.The problem is handled nicely by assigning an empty array instead of an empty string:
uploadForm.fileupload('add', { files: [[]] });
I have updated the example above.
UPDATE 2/29/16
It turned out I did want to restrict the file types after all so I modified my script to clear 'acceptFileTypes' property before the dummy file add and reset it in the add handler. Also discovered I could not access the process errors in the add handler so replaced it with 'fileuploadprocessdone' and handled the error in 'fileuploadprocessfail'.
I have updated the example above but we're still using JFU 5.42.0.
IMPORTANT
I am using 5.42.0 which is a very old version of JFU. I did not write this code and my first attempt to upgrade failed. When I do upgrade, I'll update this solution.**
I just made two separate handlers like so:
Try this puglin simpleUpload, no need form
Html:
Javascript: