On one of my pages I use multiple forms, each holds specific data (so there is a form with user's first name, last name etc. there is another with user's address and so on). Each form have a "Save changes" button, and I use Ajax calls to submit data to the server. Problem is - on one of the forms, user can upload his picture, using Bootstrap file upload (so, first he selects the picture, and then clicks "Save" - only then is the file send to server), but I can't find any way to do it without reloading the page.
First I tried using hidden iframe
, but it would seem that I can't make copy my file data to the Iframe, so this is a no go.
Now I'm trying to use this:
but, once I include it in my page, Bootstrap file uploader stops working correctly, that is - every time I select a picture it automatically starts file upload. Weird part is - it happens even when there is no init code for fileuploader anywhere, just include code. I tried suggested "fixes", that is - I tried overriding the add method like this:
$('#file').fileupload({
dataType: 'json',
add: function (e, data) {
data.context = $('<button/>').text('Upload')
.appendTo(document.body)
.click(function () {
data.context = $('<p/>').text('Uploading...').replaceAll($(this));
data.submit();
});
},
done: function (e, data) {
data.context.text('Upload finished.');
}
});
But "add" is not getting hit for some reason...
What am I doing wrong here? Or is there any other way I can achieve the thing I want?
EDIT 1:
Here is the code I use for picture upload:
@using (Html.BeginForm("ProfileSavePictureData", "Animator", FormMethod.Post, new { enctype = "multipart/form-data", id = "pictureDataForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(null, new { id = "profilePictureDataValidationSummary" })
@Html.HiddenFor(x => x.UserDataId)
@Html.HiddenFor(x => x.UserId)
<div class="form-group">
<div class="fileupload fileupload-new" data-provides="fileupload">
<input type="hidden" value="" name="" />
<div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
@if (Model == null || String.IsNullOrEmpty(Model.Picture))
{
<img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" alt="">
}
else
{
<img src="@Model.Picture" alt="@ViewBag.PictureNotAviableLabel" alt="">
@Html.HiddenFor(x => x.Picture)<br />
}
</div>
<div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px;
max-height: 150px; line-height: 2px;">
</div>
<div>
<span class="btn default btn-file"><span class="fileupload-new"><i class="fa fa-paper-clip">
</i>
@ViewBag.ChoosePicture
</span><span class="fileupload-exists"><i class="fa fa-undo"></i>
@ViewBag.ChangePicture
</span>
<input type="file" class="default" name="file" />
</span><a href="#" class="btn red fileupload-exists" data-dismiss="fileupload"><i
class="fa fa-trash-o"></i>
@ViewBag.RemovePicture
</a>
</div>
</div>
<span class="label label-danger">
@ViewBag.InfoLabel
</span><span>
@ViewBag.PictureMinatureWarning
</span>
</div>
<div class="margin-top-10">
<button id="btnSaveChengesProfilePictureData" type="button" class="btn btn-default">@ViewBag.SaveChangesButtonLabel</button>
<input type="submit" />
</div>
}
As you can see there are two buttons at the bottom - neither does what I want...