Created
August 11, 2015 13:58
-
-
Save mitallast/5fe413886218e8f0408f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2>Загрузка картинки</h2> | |
<form method="post" action="/admin/images/upload.json" enctype="multipart/form-data" id="image-upload-form" | |
data-success="/admin/images/"> | |
<div class="row"> | |
<div class="form-group col-md-6"> | |
<label for="image" class="control-label">картинка</label> | |
<input type="file" class="form-control" id="image" name="image" required/> | |
</div> | |
</div> | |
<button type="submit" class="btn btn-default">Сохранить</button> | |
</form> | |
<script> | |
$(function () { | |
var files; | |
var $form = $('#image-upload-form'); | |
var action = $form.prop('action'); | |
var success = $form.data('success'); | |
var $input = $form.find('[type=file]').first(); | |
$input.change(function (e) { | |
console.log(e); | |
files = e.target.files; | |
}); | |
$form.submit(function (e) { | |
e.preventDefault(); | |
var formData = new FormData(); | |
$.each(files, function (key, value) { | |
console.log(key, value); | |
formData.append($input.prop('name'), value); | |
}); | |
console.log(formData); | |
$.ajax({ | |
url: action, | |
type: 'POST', | |
data: formData, | |
dataType: 'json', | |
processData: false, | |
contentType: false, | |
success: function (data, textStatus, jqXHR) { | |
console.log(data, textStatus, jqXHR); | |
}, | |
error: function (jqXHR, textStatus, errorThrown) { | |
console.log(jqXHR, textStatus, errorThrown); | |
} | |
}); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment