Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional File Upload with Blueimp jquery file upload plugin

The question is related to jquery file upload plugin from blueimp

I am building a form where the file uploads are optional. It turns out I cannot post form when there are no files selected for upload. Just because of this I have to create two forms on my page and do all the dirty checking to delete uploaded files if the user decides not to add actual data.

The question is :

Can I submit the form even if no files are selected so that i can still be able to receive the additional form data on server side ?

like image 828
Roshan Gautam Avatar asked Feb 27 '13 18:02

Roshan Gautam


2 Answers

I have the same issue myself. I have a form (built in Angular), using blue imp angular implementation. The form itself has fields; the file is optional. Needs to be a single post (with or without the file).

On submit() nothing happens.

The "helpful events" listed above only are fired when a file is added.

like image 58
Tony Avatar answered Oct 13 '22 14:10

Tony


I know I am late to the party, but there was no real solution listed to date. You can fake the fact there is a file being added, by manually calling the add event like:

$('#fileupload').fileupload('add', { files: [{}] });

You would setup a variable to store the form information, update the variable when add and trigger the add like above if there was no file. Here is what the code would look like:

var fileData;
$('#fileupload').fileupload({
    add: function (e, data) {
        fileData = data;
    }
});

$('form').submit(function () {
    if (!fileData) {
        $('#fileupload').fileupload('add', { files: [{}] });
    }
    fileData.formData = params;
    fileData.submit();
    return false;
});

This allows you to stay consistent with how the data is passed to the server.

like image 39
fanfavorite Avatar answered Oct 13 '22 13:10

fanfavorite